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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
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

Add a Patch

Pull Requests

Add a Pull Request

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-2024 The PHP Group
All rights reserved.
Last updated: Sun May 05 16:01:30 2024 UTC