php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #34506 would like an array_key_value_pop function
Submitted: 2005-09-15 04:14 UTC Modified: 2012-04-14 02:59 UTC
Votes:2
Avg. Score:4.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: shrub at yahoo dot com Assigned:
Status: Wont fix Package: *General Issues
PHP Version: 5.1.0RC1 OS: n/a
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: shrub at yahoo dot com
New email:
PHP Version: OS:

 

 [2005-09-15 04:14 UTC] 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

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-04-13 20:39 UTC] nikic@php.net
-Status: Open +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: nikic
 [2012-04-13 20:39 UTC] nikic@php.net
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.
 [2012-04-14 02:59 UTC] aharvey@php.net
-Status: Closed +Status: Wont fix -Assigned To: nikic +Assigned To:
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Jul 07 05:01:36 2025 UTC