Re: array_pop() with key-value pair ???
| From: | Németh Zoltán | Date: | Fri, 16 Feb 2007 11:01:53 +0000 |
| Subject: | Re: array_pop() with key-value pair ??? | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-248955@lists.php.net to get a copy of this message | ||
2007. 02. 16, péntek keltezéssel 10.47-kor Robin Vickery ezt írta:
> On 16/02/07, Németh Zoltán <znemeth@alterationx.hu> wrote:
> > 2007. 02. 16, péntek keltezéssel 10.23-kor Robin Vickery ezt írta:
> > > On 16/02/07, Eli <eli.hen@gmail.com> wrote:
> > > > Hi,
> > > >
> > > > Why isn't there a function that acts like array_pop() returns a pair of
> > > > key-value rather than the value only ?
> > > >
> > > > Reason is, that in order to pop the key-value pair, you do:
> > > > <?php
> > > > $arr =
> > > > array('a'=>1,'b'=>2,'c'=>3,'d'=>4);
> > > > $arr_keys = array_keys($arr);
> > > > $key = array_pop($arr_keys);
> > > > $value = $arr[$key];
> > > > ?>
> > > >
> > > > I benchmarked array_keys() function and it is very slow on big arrays
> > > > since PHP has to build the array.
> > >
> > > benchmark this:
> > >
> > > end($arr);
> > > list($key, $value) = each($arr);
> >
> > array_pop also removes the element from the array so add one more line:
> > unset($arr[$key]);
>
> Yeah, but he's not actually popping the last element of $arr, he's
> just using it to get the last key from $arr_keys.
well, sorry, I didn't examine his code just read his original question
saying "a function that acts like array_pop()"
it's now up to him to decide whether he wants to unset that element or
not :)
greets
Zoltán Németh
>
> -robin
>