|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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 PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Oct 26 15:00:01 2025 UTC |
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; }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");