在嵌套对象php上执行功能

I have a structure of nested objects (menu items that relate to a hierarchical list) in this format:

class MenuStructure{
     public menuItems = array() ; 
}

class MenuItem{
    public menuItems = array() ;
}

each menu item can have many sub items nested indefinitely.

What is the simplest way for me to apply a particular function to ALL nested menu items in the structure, or to remove a particular item from anywhere within the structure based on some kind of checking requirement.

Is there a similar technique to array_walk_recursive?

Ok, after some playing around arrived at a solution which may be helpful to anyone else in this situation.

Applying a function to each item, and unsetting an item based on a function required two different approaches, because unsetting the item needs to be done at the parent level rather than via reference to the item itself. Both take an anonymous function as a parameter.

The first function loops all items and subitems and applies the function directly to the items. The second function takes an anonymous match function that returns true or false, and unsets child items based on whether or not they match.

class MenuStructure{

   public items = array() ;

   public function applyFunctionToItems($function){
        foreach($this->items as $key => $item){
            $item->applyFunction($function) ;
            $item->applyFunctionToItems($function) ;
        }
    }

   public function unsetMatchingItems($match){
        foreach($this->items as $key => $item){
            if($match($item)){
                unset($this->items[$key]) ;
            } ;
            $item->unsetMatchingItems($match) ;
        }
    }

}

class MenuItem{

   public items = array();

    public function applyFunction($function){
        $function($this) ;
    }

    public function applyFunctionToItems($function){
        foreach($this->items as $item){
            $item->applyFunction($function) ;
            $item->applyFunctionToItems($function) ;
        }           
    }

    public function unsetMatchingItems($match){
        foreach($this->items as $key => $item){
            if($match($item)){
                unset($this->items[$key]) ;
            };
            $item->unsetMatchingItems($match) ;
        }
    }

}

//Use as follows:

$menu = new MenuStructure() ;
$menu -> populate() ;

$menu->applyFunctionToItems(function($item){

   //Sets the name of all nested items in menu to 'Dave'

   $item->name = "Dave" ;

}) ;


$menu->menuStructure->unsetMatchingItems(function($item){

    //Removes any items named 'Chris' from all levels of nesting in menu

    if($item->name == "Chris"){
        return true;
    }else{
        return false;
    }

}) ;