Re: [PHP] Re: How do I remove an array element from within a recursive function? [solved]

2009-02-27 Thread Daevid Vincent
OMG! How retarded am I. Duh. of course... For some reason I thought you
could use the & reference because it was a "pointer" to the actual
object

menuItem::removeMenuItems($navArray['dart'], array('Login',
'Lost Password'));

public static final function removeMenuItems(&$menuItems, $removeArray)
{
foreach($menuItems as $key => $value)
if (is_array($value->children))
menuItem::removeMenuItems($value->children, 
$removeArray);
elseif (in_array($value->menu, $removeArray))
unset($menuItems[$key]);
}



-Original Message-
From: Chris 
To: Daevid Vincent 
Cc: php-general@lists.php.net, Shawn McKenzie 
Subject: Re: [PHP] Re: How do I remove an array element from within a
recursive function?
Date: Fri, 27 Feb 2009 13:47:38 +1100


Daevid Vincent wrote:
> 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);
> }
> }

You don't unset the value, you unset the key.

 $value) {
if ($value == 'menu2') {
unset($items[$_menuKey]);
}
}

echo "After:\n";
print_r($items);


$ php test.php
Before:
Array
(
 [0] => menu1
 [1] => menu2
 [2] => menu3
)
After:
Array
(
 [0] => menu1
 [2] => menu3
)





RE: [PHP] Re: How do I remove an array element from within a recursive function?

2009-02-27 Thread Boyd, Todd M.
> -Original Message-
> From: Chris [mailto:dmag...@gmail.com]
> Sent: Thursday, February 26, 2009 8:48 PM
> To: Daevid Vincent
> Cc: php-general@lists.php.net; Shawn McKenzie
> Subject: Re: [PHP] Re: How do I remove an array element from within a
> recursive function?
> 
> Daevid Vincent wrote:
> > 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);
> > }
> > }
> 
> You don't unset the value, you unset the key.
> 
>  
> $items = array('menu1', 'menu2', 'menu3');
> 
> echo "Before:\n";
> print_r($items);
> 
> foreach ($items as $_menuKey => $value) {
>   if ($value == 'menu2') {
>   unset($items[$_menuKey]);
>   }
> }
> 
> echo "After:\n";
> print_r($items);
> 
> 
> $ php test.php
> Before:
> Array
> (
>  [0] => menu1
>  [1] => menu2
>  [2] => menu3
> )
> After:
> Array
> (
>  [0] => menu1
>  [2] => menu3
> )

I would have gone with array_splice() on this one, I think. However, I
believe it will mangle associative arrays. Glad to know that there's a
way to use unset() on these, as I was unaware of it.

<3 this list! :D

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



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

2009-02-26 Thread Chris

Daevid Vincent wrote:

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);
}
}


You don't unset the value, you unset the key.

 $value) {
if ($value == 'menu2') {
unset($items[$_menuKey]);
}
}

echo "After:\n";
print_r($items);


$ php test.php
Before:
Array
(
[0] => menu1
[1] => menu2
[2] => menu3
)
After:
Array
(
[0] => menu1
[2] => menu3
)

--
Postgresql & php tutorials
http://www.designmagick.com/


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



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

2009-02-26 Thread Daevid Vincent
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 
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