php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #62979 Re-Request of additional function "array_remove($needle, $haystack)""
Submitted: 2012-08-30 23:28 UTC Modified: 2012-08-31 00:14 UTC
Votes:2
Avg. Score:5.0 ± 0.0
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:1 (50.0%)
From: kay dot luedtke at web dot de Assigned:
Status: Wont fix Package: Arrays related
PHP Version: 5.4.6 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: kay dot luedtke at web dot de
New email:
PHP Version: OS:

 

 [2012-08-30 23:28 UTC] kay dot luedtke at web dot de
Description:
------------
I'd like to re-request the function
  array_remove($needle, $haystack);
for the deletion of one/multiple array element/s from an existing array.

Similar to Bug #12129 (by Dave Mertens), but:


(zak) "[...]
       Use: unset($array['key']);"

(Kay) "What if I do not know the key, but still want to delete every occurrence of array elements where
       only the value - not key - is known (and of course keeping the original arrays order)?.
       This would - in my opinion as well as Mr. Mertens' - complete the native functions for arrays."


Example-Input:
--------------
$needle = "red";
$haystack = Array("foo"=>"bar", 0=>"red", "would"=>"be cool");
array_remove($needle, $haystack) --> Array("foo"=>"bar", "would"=>"be cool");

$needle = Array("be cool", 0xB16B00B5);
$haystack = Array("foo"=>"bar", 8=>0xB16B00B5, "would"=>"be cool");
array_remove($needle, $haystack) --> Array("foo"=>"bar");


My sample function (or this request for that matter) may not be perfect.
But please be kind - it's 01:15 AM in Germany and I have to get up for work in about 5 hours...
 

Test script:
---------------
function array_remove($needle, Array $hackstack)
{
	if(empty($hackstack)) return Array();
	if(!is_array($needle) && !is_string($needle) &&
           !is_numeric($needle)) return $hackstack;

	$new_array = Array();
	foreach($hackstack as $key=>$value)
	{
		if(is_array($needle))
		{
			$found = FALSE;
			foreach($needle as $needle2)
			{
				if((string)$needle2 == (string)$value)
					$found = TRUE;
			}
			if(!$found) $new_array[$key] = $value;
		}
		else
		{
			if((string)$needle != (string)$value)
				$new_array[$key] = $value;
		}
	}

	return $new_array;
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-08-30 23:29 UTC] kay dot luedtke at web dot de
Edit because of E-Mail-Change
 [2012-08-30 23:29 UTC] kay dot luedtke at web dot de
-: KayL at cust dot in +: kay dot luedtke at web dot de
 [2012-08-31 00:13 UTC] rasmus@php.net
-Status: Open +Status: Wont fix
 [2012-08-31 00:13 UTC] rasmus@php.net
This can be done in a simple 4-line function:

function array_remove($needles, &$haystack, $strict = false) {
    if(!is_array($needles)) $needles = array($needles);
    foreach($needles as $needle) 
        while (false !== ($index = array_search($needle, $haystack, $strict)))
            unset($haystack[$index]);
}
 [2012-08-31 00:14 UTC] rasmus@php.net
Actually, 3 lines. We can just cast the arg to an array:

function array_remove($needles, &$haystack, $strict = false) {
    foreach((array)$needles as $needle) 
        while (false !== ($index = array_search($needle, $haystack, $strict)))
            unset($haystack[$index]);
}
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Jun 23 19:01:23 2021 UTC