Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62355 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 34017 invoked from network); 21 Aug 2012 20:33:40 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2012 20:33:40 -0000 Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.170 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 74.125.82.170 mail-we0-f170.google.com Received: from [74.125.82.170] ([74.125.82.170:45646] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 70/F4-10139-3A0F3305 for ; Tue, 21 Aug 2012 16:33:40 -0400 Received: by weyr1 with SMTP id r1so162087wey.29 for ; Tue, 21 Aug 2012 13:33:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type; bh=BNLVfKTfv3TSePec7Yl2k3Y8yxZQIrWueeijOOH4Q+I=; b=OnBk3aI7iM4Eoxs0LZJaGpPLtkL2lvS6KzAC40nAw21C8Y/+kZJD1U5+oqlW9PruX/ nO9MiW9/NijwEcUsKhsji3r3fIBe4HC0H56vSMgDzxepH1Y/MqOihDzPWWULy8KuqNEm Dx1WjJdd4RRMLAgKKT3GF0QrUszgMNNo6ZCVRPG2DxlrBpjtY9lo4tR9vwPbALPxdXB1 SpxKfICxWsQJIZJqELxz1OCff71fj3c1GCuU2s3FbfOF9vz4tYanj3lRwXv0498wzQXJ Yyfr6fHCx4E8xV8voZ4djIUNvb+uxkapCRDwfYNQlqE2ehCx1YePJ0hqZJBX4qRhChM+ jaXg== Received: by 10.180.105.6 with SMTP id gi6mr40828391wib.4.1345581216219; Tue, 21 Aug 2012 13:33:36 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.223.86.201 with HTTP; Tue, 21 Aug 2012 13:32:55 -0700 (PDT) In-Reply-To: <5033EAAB.6090502@ajf.me> References: <502EB667.1020602@lerdorf.com> <5033E0BC.8040507@ajf.me> <5033EAAB.6090502@ajf.me> Date: Wed, 22 Aug 2012 05:32:55 +0900 X-Google-Sender-Auth: 7TsqE9a_qWvw-35FJcgmGLgQwW0 Message-ID: To: Andrew Faulds Cc: Tjerk Anne Meesters , Rasmus Schultz , internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] re: removing an item from an array From: yohgaki@ohgaki.net (Yasuo Ohgaki) 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. > > 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 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/ >