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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Shawn McKenzie wrote:
> 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);
>> 				}
>> 			}
>> 		}
>> 	}
>>
> 
> 

Also, I'm not sure what happens here:

if (is_array($value->children))
	menuItem::removeMenuItems(&$value->children, $removeArray);
else
{

Your function defines that var as a reference so you don't have to use a
reference in the call.  Don't know if it does anything but throw a
deprecated notice though.

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux