php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #40101 Please add an array_remove function
Submitted: 2007-01-11 20:45 UTC Modified: 2007-01-11 21:15 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:2 (100.0%)
Same OS:1 (50.0%)
From: wgilk at reliablesites dot com Assigned:
Status: Wont fix Package: Feature/Change Request
PHP Version: 6CVS-2007-01-11 (CVS) OS: ALL
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: wgilk at reliablesites dot com
New email:
PHP Version: OS:

 

 [2007-01-11 20:45 UTC] wgilk at reliablesites dot com
Description:
------------
It would be very nice to have an array_remove (or some other 
name) function to do what the code below illustrates.

The point of the function is to allow use to retrieve the 
value of a particular key in an array, and remove it from the 
array at the same time. Think of it as array_shift(), but with 
the ability to define the key that gets shifted.

Reproduce code:
---------------
function array_remove ($key, &$array) {
	if (! isset ($array[$key])) return false;

	$return = $array[$key];
	unset ($array[$key]);
	
	return $return;		
}

$array = array ('food' => 'taco', 'drink' => 'juice');

$value = array_remove ('food', $array);

print_r ($array);
print $value;

Expected result:
----------------
FROM print_r:
----
array (
  'drink' => 'juice
)
----

FROM print:
----
taco
----


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-01-11 20:52 UTC] tony2001@php.net
You can use the function from your example instead.
Adding more basic functions to the core doesn't make any sense.
 [2007-01-11 20:57 UTC] wgilk at reliablesites dot com
But there is no function to do this in php. I relize it's 
trivial, but it would still be very use in many applications.
 [2007-01-11 21:15 UTC] tony2001@php.net
That's what PHP is about - it's a language, not just a limited stack of tools.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 10:01:32 2024 UTC