Re: re: removing an item from an array
| From: | Andrew Faulds | Date: | Tue, 21 Aug 2012 20:08:11 +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 15 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-62353@lists.php.net to get a copy of this message | ||
On 21/08/12 21:00, Yasuo Ohgaki wrote:
Hi 2012/8/22 Andrew Faulds <ajf@ajf.me>:On 21/08/12 10:36, Yasuo Ohgaki wrote:Original proposal was to delete all value that matches. It's not delete a single value. Treats it like a set, removes all but in practice only one will go if properly formed set.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:
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. OK.
We already have array_walk() and array_filter().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] Yes, but you seem to be ignoring me. I don't want that, because then I can't remove items a set containing strings or other callables, using this function.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.
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-- Andrew Faulds http://ajf.me/