2012/8/20 Andrew Faulds <a...@ajf.me>:
> On 20/08/12 00:16, Yasuo Ohgaki wrote:
>>
>> 2012/8/20 Andrew Faulds <a...@ajf.me>:
>>>
>>> On 20/08/12 00:05, Yasuo Ohgaki wrote:
>>>>
>>>> 2012/8/20 Etienne Kneuss <col...@php.net>:
>>>>>
>>>>> On Sun, Aug 19, 2012 at 11:57 PM, Yasuo Ohgaki <yohg...@ohgaki.net>
>>>>> wrote:
>>>>>>
>>>>>> 2012/8/18 Rasmus Lerdorf <ras...@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; })
>>>>>
>>>>> So array_filter?
>>>>
>>>> I'll use it or like for deleting, but the point of this thread is
>>>> "intuitive function for deleting element(s)"
>>>>
>>>> array_delete($array, $value|callable)
>>>>
>>>> would be nicer for users, perhaps.
>>>
>>> A callable? Wouldn't that mean you couldn't delete strings? :(
>>
>> If you read my previous post,  you'll see why it would be nicer with
>> callable.
>
> Being unable to remove callables from an array would be very strange, and I
> think for the use case you presented array_filter works fine.

Of course it works.

$array = array_filter($array, function($v) use ($del_val) {
    return ($v !== $del_val);
});

It does not delete element(s), but creates new array.

void array_delete ( array &$input [, callable $callback = "" |
$values_to_be_deleted ] )

This will delete element(s).
We can use array_walk() to delete element(s) from array also.

php > $array = array(1,2,3,4,5);
php > array_walk($array, function($val, $key) use (&$array) {if ($val
== 3) unset($array[$key]);});
php > var_dump($array);
array(4) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [3]=>
  int(4)
  [4]=>
  int(5)
}

I suppose this isn't an intuitive way for novices because stack
overflow's answers
didn't have this method that actually delete element(s) from an array.

Adding an intuitive element deletion function is the point of this
thread, isn't it?
I'll vote +1 for adding array_delete().

Regards,

--
Yasuo Ohgaki

>
>>>>>> 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
>
>
>
> --
> Andrew Faulds
> http://ajf.me/
>

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to