Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62356 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35510 invoked from network); 21 Aug 2012 20:39:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2012 20:39:40 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Received-SPF: pass (pb1.pair.com: domain ajf.me designates 64.22.89.134 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.134 oxmail.registrar-servers.com Received: from [64.22.89.134] ([64.22.89.134:34233] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 26/45-10139-B02F3305 for ; Tue, 21 Aug 2012 16:39:40 -0400 Received: from [192.168.0.200] (5ad3285b.bb.sky.com [90.211.40.91]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id BE03AF002B; Tue, 21 Aug 2012 16:39:34 -0400 (EDT) Message-ID: <5033F1EA.4070701@ajf.me> Date: Tue, 21 Aug 2012 21:39:06 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Yasuo Ohgaki CC: Tjerk Anne Meesters , Rasmus Schultz , internals@lists.php.net References: <5033E0BC.8040507@ajf.me> <5033EAAB.6090502@ajf.me> In-Reply-To: Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] re: removing an item from an array From: ajf@ajf.me (Andrew Faulds) On 21/08/12 21:32, Yasuo Ohgaki wrote: > 2012/8/22 Andrew Faulds : >> On 21/08/12 21:00, Yasuo Ohgaki wrote: >>> Hi >>> >>> 2012/8/22 Andrew Faulds : >>>> 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. Yes, but I think this completely destroys the convenience of it. You might as well just use array_walk if it only supports callable. And array_walk is fine. array_delete should remove a value, not values according to a condition. > >> 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. Er, so you're advocating adding another method to do the same thing? Why? The whole point of this was to allow people to use arrays like sets, or easily remove list items. Not encourage people to use functional programming. Sure, people should use array_walk, but you've got to be kidding me if I you think I would honestly accept using this to remove array items by value: array_delete($array, function ($value) { return ($value == "that value"); }); I might as well use array_walk if we're going down this route. You seem to want to subvert the usefulness of this function and just provide an array_walk shorthand with less functionality. What I want is this: array_delete($array, "that value"); Much more readable, does what you expect, and provides a significant, noticeable value over array_walk here. > > Regards, > > -- > Yasuo Ohgaki > yohgaki@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 >>> yohgaki@ohgaki.net >> >> >> -- >> Andrew Faulds >> http://ajf.me/ >> -- Andrew Faulds http://ajf.me/