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.

                        $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");
                        $fieldARR = array();
                            //parent is used to check if a field is used in a repeater
                            foreach($fields as $field){
                            }//foreach($fields as $field){
                        //get all layouts and put in array
                        $layouts = $modx->getIterator("cbLayout");
                        $layoutARR = array();
                            foreach($layouts as $layout){
                            }//foreach($fields as $field){
                        //go through all resources and unset used layouts and fields
                        $resources = $modx->getIterator("modResource");
                            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){
                                    //unset used fields
                                }//end foreach
                            }//foreach($resources as $resource){
                        $output.= '<h1>Unused layouts</h1>
                        $output.= print_r($layoutARR,true);
                        $output.= '</pre>';
                        $output.= '<h1>Unused velden</h1>
                        $output.= print_r($fieldARR,true);
                        $output.= '</pre>';
                        return $output;