Re: re: removing an item from an array
| From: | Yasuo Ohgaki | Date: | Sun, 19 Aug 2012 21:57:27 +0000 |
| Subject: | Re: re: removing an item from an array | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62265@lists.php.net to get a copy of this message | ||
2012/8/18 Rasmus Lerdorf <rasmus@lerdorf.com>:
> On 08/17/2012 05:21 PM, Rasmus Schultz wrote:
>>>
>>> if(($key = array_search($del_val, $messages)) !== false) {
>>> unset($messages[$key]);
>>> }
>>>
>>> Nothing horrible here.
>>>
>>
>> I disagree - this is (or should be) a simple, atomic operation...
>> yet, you've got a function-call, an intermediary variable, a boolean test,
>> and an unset statement repeating the name of the array you're deleting from.
>>
>> This should be a simple statement or function/method-call, and in most
>> other languages it would be...
>
> Really? I can't think of a single language that has a call to remove an
> element by value in a key-value hash. Do you have some examples? What do
> you do with duplicates?
>
Ruby can do (using irb)
ruby-1.9.2-p180 :007 > h = {"apple"=>150, "banana"=>300,
"lemon"=>300}
=> {"apple"=>150, "banana"=>300, "lemon"=>300}
ruby-1.9.2-p180 :008 > h.delete_if { |k,v| v==300 }
=> {"apple"=>150}
May be we should have something like
array_delete_if($array, function($v, $k=null) { if ($v == 300) return true; })
--
Yasuo Ohgaki
yohgaki@ohgaki.net