Snippet to check for unused fields and layouts

I’ve written a small snippet to check for unused fields and layouts. We use it to see which fields and layouts can be removed to make content management less confusing for our clients. If there are improvements/better ways to code it, just let me know.
<?php
$output="";

                        $cbCorePath = $modx->getOption('contentblocks.core_path', null, $modx->getOption('core_path').'components/contentblocks/'); 
                        $ContentBlocks = $modx->getService('contentblocks','ContentBlocks', $cbCorePath.'model/contentblocks/');
                                
                        //get all fields and put in array
                        $fields = $modx->getIterator("cbField");
                        $fields->rewind();
                        
                        $fieldARR = array();
                        if($fields->valid()){
                        
                            //parent is used to check if a field is used in a repeater
                            foreach($fields as $field){
                                $fieldARR[$field->id]=array("name"=>$field->name,"parent"=>$field->parent);
                            }//foreach($fields as $field){
                        }//if($fields->valid()){
                        
                        
                        //get all layouts and put in array
                        $layouts = $modx->getIterator("cbLayout");
                        $layouts->rewind();
                        
                        $layoutARR = array();
                        if($layouts->valid()){
                            foreach($layouts as $layout){
                                $layoutARR[$layout->id]=$layout->name;
                            }//foreach($fields as $field){
                        }//if($fields->valid()){
                        
                        
                        //go through all resources and unset used layouts and fields
                        $resources = $modx->getIterator("modResource");
                        $resources->rewind();
                        if($resources->valid()){
                            foreach($resources as $resource){
                                $properties = $modx->fromJSON($resource->properties);
                                $contents = $modx->fromJSON($properties["contentblocks"]["content"]);
                                
                                //unset used layouts
                                foreach($contents as $content)unset($layoutARR[$content["layout"]]);
                                
                                //unset used fields
                                foreach($properties["contentblocks"]["fieldcounts"] as $id=>$name){
                                    //unset fields used in CB repeater
                                    foreach($fieldARR as $key=>$val){
                                        if($val["parent"]==$id)unset($fieldARR[$key]);
                                    }
                                    //unset used fields
                                    unset($fieldARR[$id]);
                                }//end foreach
                            }//foreach($resources as $resource){
                        }//if($resources->valid()){
                        
                        
                        $output.= '<h1>Unused layouts</h1>
                                    <pre>';
                        $output.= print_r($layoutARR,true);
                        $output.= '</pre>';
                        
                        $output.= '<h1>Unused velden</h1>
                                    <pre>';
                        $output.= print_r($fieldARR,true);
                        $output.= '</pre>';
                        
                        return $output;