Edit report at https://bugs.php.net/bug.php?id=62979&edit=1
ID: 62979
User updated by: kay dot luedtke at web dot de
Reported by: kay dot luedtke at web dot de
Summary: Re-Request of additional function
"array_remove($needle, $haystack)""
Status: Open
Type: Feature/Change Request
Package: Arrays related
Operating System: All
PHP Version: 5.4.6
Block user comment: N
Private report: N
New Comment:
Edit because of E-Mail-Change
Previous Comments:
------------------------------------------------------------------------
[2012-08-30 23:28:27] 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;
}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=62979&edit=1