Re: re: removing an item from an array
| From: | Yasuo Ohgaki | Date: | Tue, 21 Aug 2012 20:00:29 +0000 |
| Subject: | Re: re: removing an item from an array | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62352@lists.php.net to get a copy of this message | ||
Hi
2012/8/22 Andrew Faulds <ajf@ajf.me>:
> On 21/08/12 10:36, Yasuo Ohgaki wrote:
>>
>> Int would be better and callable should be accepted like array_walk().
>> It's better to have array_delete_recursive(), too.
>> I updated the page.
>
> Callable? What? This is to remove a single value, like a set. If you want to
> remove based on a function, array_walk() is always available. And allowing a
> callable would prevent you from removing closures or strings! D:
Original proposal was to delete all value that matches.
It's not delete a single value.
We could choose to remove only 1st element, but is it useful?
People may do
while (array_delete($array, "do not needed"));
This is stupid O(n^2) code.
>
>>
>> array_add() needs more discussion.
>> What we should do with array value, accept callable or not, etc.
>
> Why accept a callable?
> I don't think it needs more discussion, I can't see how it could be made any
> better than it currently is.
What's the point of adding NEW feature with less extensible against competitor?
Take a look at Ruby's delete_if()
With callable, one could do
$array = [1,2,3,4,5,6,7,8];
$cnt = array_delete($array, function($v) { if ($v <== 4) return true; });
var_dump($cnt, $array); // $cnt = 4, $array = [5,6,7,8]
With array_walk()
$array = [1,2,3,4,5,6,7,8];
$cnt = 0;
array_walk($array, function($v) use (&$array, &$cnt) { if ($v <== 4)
{$cnt++; return true;) });
var_dump($cnt, $array); // $cnt = 4, $array = [5,6,7,8]
As I mentioned earlier, array_walk() is the best way to delete elements with PHP
more than a decade. It should be mentioned the Stack Overflow page,
but it's not.
It's just like adding
array_pop()/array_push()/array_shift()/array_unshift() while
we have array_slice()/array_splice().
Regards,
--
Yasuo Ohgaki
yohgaki@ohgaki.net