Req #34506 [Opn->Csd]: would like an array_key_value_pop function
| From: | nikic@php.net | Date: | Fri, 13 Apr 2012 20:39:07 +0000 |
| Subject: | Req #34506 [Opn->Csd]: would like an array_key_value_pop function | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-169558@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=34506&edit=1
ID: 34506
Updated by: nikic@php.net
Reported by: shrub at yahoo dot com
Summary: would like an array_key_value_pop function
-Status: Open
+Status: Closed
Type: Feature/Change Request
-Package: Feature/Change Request
+Package: *General Issues
Operating System: n/a
PHP Version: 5.1.0RC1
-Assigned To:
+Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Closing this as this is already possible simply by using array_splice:
$a = array ("red"=>"12a", "fish" => "rock",
"blue" => "xyz");
var_dump(array_splice($a, -1));
var_dump(array_splice($a, -1));
var_dump(array_splice($a, -1));
The pop operation is intended for use with stacks and you don't need the key in that case.
Previous Comments:
------------------------------------------------------------------------
[2005-09-15 04:14:56] shrub at yahoo dot com
Description:
------------
When you call array_pop on an array, you only get back the
value. It would be nice if there was an array_key_value_pop
function that returned both the key and the value.
Reproduce code:
---------------
function array_key_value_pop (&$a) {
end($a);
$lastItem = each($a);
array_pop($a);
return array($lastItem["key"] => $lastItem["value"]);
}
Expected result:
----------------
$a = array ("red"=>"12a", "fish" => "rock",
"blue" => "xyz");
print_r (array_key_value_pop($a));
Array
(
[blue] => xyz
)
print_r($a);
Array
(
[red] => 12a
[fish] => rock
)
Actual result:
--------------
n/a
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=34506&edit=1