php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12129 array_remove()
Submitted: 2001-07-13 03:21 UTC Modified: 2001-07-13 03:44 UTC
From: dmertens at zyprexia dot com Assigned:
Status: Not a bug Package: Feature/Change Request
PHP Version: 4.0.6 OS: Linux
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: dmertens at zyprexia dot com
New email:
PHP Version: OS:

 

 [2001-07-13 03:21 UTC] dmertens at zyprexia dot com
I would like to see an function called array_remove(needle, haystack) with removes an item from an associative (or indexed) array.

Currently i have a workaround which loops the array. During the loop a new array is created. When the loop finds the item which has to be removed, it skip the current item.

After the loop, the new array is returned. A function like this would complete the array functions.

Dave Mertens
dmertens@zyprexia.com

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-07-13 03:44 UTC] zak@php.net
Thank you for the suggestion! However this functionality is already part of PHP.

Use:
   unset ($array['key']);
 [2012-08-30 19:14 UTC] KayL at cust dot in
No it is not.
What if I do not know the key, but want to delete exactly that array element?


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

$needle = Array("would be", 0);
$hackstack = Array("foo"=>"bar", 0=>"red", "would be"=>"cool");
array_remove($needle, $hackstack) = Array("foo"=>"bar");


Function:
---------
function array_remove(mixed $needle, Array $hackstack)
{
    if(empty($hackstack)) return Array();

    $new_array = Array();
    foreach($hackstack as $key=>$value)
    {
        if($value != $needle)
        {
            $new_array[$key] = $value;
        }
    }
    return $new_array;
}
 [2012-08-30 19:20 UTC] KayL at cust dot in
Fcuk. Ofc I meant:


$needle = "bar";
$hackstack = Array("foo"=>"bar", 0=>"red", "would be"=>"cool");
array_remove($needle, $hackstack) = Array(0=>"red", "would be"=>"cool");

$needle = Array("cool", 0xB16B00B5);
$hackstack = Array("foo"=>"bar", 8=>0xB16B00B5, "would be"=>"cool");
array_remove($needle, $hackstack) = Array("foo"=>"bar");
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jan 23 12:01:30 2025 UTC