Re: Re: How do I remove an array element from within a recursive function?

From: Date: Fri, 27 Feb 2009 02:40:01 +0000
Subject: Re: Re: How do I remove an array element from within a recursive function?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-288897@lists.php.net to get a copy of this message
I tried that and it still doesn't work. I even tried this hardcore "test": public static final function removeMenuItems(&$menuItems, $removeArray) { foreach($menuItems as &$value) { unset($value); } } -----Original Message----- From: Shawn McKenzie <nospam@mckenzies.net> To: php-general@lists.php.net Subject: [PHP] Re: How do I remove an array element from within a recursive function? Date: Thu, 26 Feb 2009 20:10:20 -0600 Daevid Vincent wrote: > I'm trying to remove ["menu"] == 'Login' from the array, but despite "finding" the element, it never removes. > isn't that what the & reference stuff is for? Yes, but foreach can't modify an array unless you use a reference in the foreach also. Try this: foreach($menuItems as &$value) > > menuItem::removeMenuItems($navArray['dart'], array('Login', 'Lost > Password')); > > > public static final function removeMenuItems(&$menuItems, $removeArray) > { > foreach($menuItems as $value) > { > if (is_array($value->children)) > menuItem::removeMenuItems(&$value->children, $removeArray); > else > { > //echo "*** CHECKING ".$value->menu." against > ".implode(',',$removeArray)." ***"; > if (in_array($value->menu, $removeArray)) > { > //echo "*** REMOVING ".$value->menu." ***"; > unset($value); > } > } > } > } > -- Thanks! -Shawn http://www.spidean.com

« previous php.general (#288897) next »