RE: [PHP] Re: How do I remove an array element from within a recursive function?
| From: | Boyd, Todd M. | Date: | Fri, 27 Feb 2009 14:16:30 +0000 |
| Subject: | RE: [PHP] Re: How do I remove an array element from within a recursive function? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-288918@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Chris [mailto:dmagick@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.
>
> <?php
>
> $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