2012/8/22 Andrew Faulds <a...@ajf.me>: > On 21/08/12 21:00, Yasuo Ohgaki wrote: >> >> Hi >> >> 2012/8/22 Andrew Faulds <a...@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. > > Treats it like a set, removes all but in practice only one will go if > properly formed set. > >> 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. > >> >>>> 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] > > 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.
All element value and key are passed to function, you may remove any type of data if array_delete() accepts only callable. That's the reason why I suggest to accept callable only. Users should get used to closure/functional programming anyway. > > We already have array_walk() and array_filter(). > >> 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] Oops, it should unset(). $array = [1,2,3,4,5,6,7,8]; $cnt = 0; array_walk($array, function($v, $k) use (&$array, &$cnt) { if ($v <== 4) {$cnt++; unset($array[$k];} }); var_dump($cnt, $array); // $cnt = 4, $array = [5,6,7,8] The problem is "Many PHP users do not know the best way to delete elements, while there is array_walk() for a long long time.", isn't it? Providing an intuitive and easier API for basic operation should be the solution, not providing simple method. Regards, -- Yasuo Ohgaki yohg...@ohgaki.net >> >> 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 >> yohg...@ohgaki.net > > > > -- > Andrew Faulds > http://ajf.me/ > -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php