Discussion:
array_pop() with key-value pair ???
Eli
2007-02-16 06:58:44 UTC
Permalink
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. While array_pop() can be acceleraed by
the guts of PHP engine.

-thanks
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eli
2007-02-16 07:14:08 UTC
Permalink
More over.. PHP seems to be quiet slow when dealing with array_shift()
array_unshift(), but it is much faster with array_pop() array_push(). I
am not familiar with the PHP internals, but why not add a pointer to the
start and end of the array?

Good word can be said on PHP that accelerated at least count() function..

Maybe I require too much.. PHP is a rapid development scripting
language.. not a massive optimized programming language.. :-/
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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. While array_pop() can be acceleraed by
the guts of PHP engine.
-thanks
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Mikey
2007-02-16 09:44:58 UTC
Permalink
I guess you have tried foreach?

foreach ($array as $key => $value) {
...
}

Mikey
Post by Eli
More over.. PHP seems to be quiet slow when dealing with array_shift()
array_unshift(), but it is much faster with array_pop() array_push(). I
am not familiar with the PHP internals, but why not add a pointer to the
start and end of the array?
Good word can be said on PHP that accelerated at least count() function..
Maybe I require too much.. PHP is a rapid development scripting
language.. not a massive optimized programming language.. :-/
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair
of key-value rather than the value only ?
<?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. While array_pop() can be acceleraed
by the guts of PHP engine.
-thanks
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eli
2007-02-16 14:07:51 UTC
Permalink
Post by Mikey
I guess you have tried foreach?
foreach ($array as $key => $value) {
...
}
No.. loop should not be necessary when you want to take only the first
or the last element in the array.
Better using array_pop() array_shift() reset() end() and each()
functions for better run times.
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robin Vickery
2007-02-16 10:23:35 UTC
Permalink
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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);

-robin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Németh Zoltán
2007-02-16 10:38:19 UTC
Permalink
Post by Robin Vickery
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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.
end($arr);
list($key, $value) = each($arr);
array_pop also removes the element from the array so add one more line:
unset($arr[$key]);

greets
Zoltán Németh
Post by Robin Vickery
-robin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Robin Vickery
2007-02-16 10:47:18 UTC
Permalink
Post by Németh Zoltán
Post by Robin Vickery
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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.
end($arr);
list($key, $value) = each($arr);
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.

-robin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Németh Zoltán
2007-02-16 11:01:53 UTC
Permalink
Post by Robin Vickery
Post by Németh Zoltán
Post by Robin Vickery
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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.
end($arr);
list($key, $value) = each($arr);
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
Post by Robin Vickery
-robin
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Eli
2007-02-16 14:01:27 UTC
Permalink
Post by Robin Vickery
Post by Eli
Hi,
Why isn't there a function that acts like array_pop() returns a pair of
key-value rather than the value only ?
<?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.
end($arr);
list($key, $value) = each($arr);
Thanks! that benchmarks with normal speed.. :-)
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Continue reading on narkive:
Loading...