Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62352 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27519 invoked from network); 21 Aug 2012 20:01:27 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2012 20:01:27 -0000 Authentication-Results: pb1.pair.com smtp.mail=yohgaki@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=yohgaki@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.54 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 74.125.82.54 mail-wg0-f54.google.com Received: from [74.125.82.54] ([74.125.82.54:60969] helo=mail-wg0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 41/73-10139-619E3305 for ; Tue, 21 Aug 2012 16:01:27 -0400 Received: by wgx1 with SMTP id 1so139549wgx.11 for ; Tue, 21 Aug 2012 13:01:24 -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=5khDq2H2u018c8XqShZ/OgTlH6SuTvNgH1V8aMDXO2o=; b=ZevO+EsMKt5VTnBxIXrdUUCSs7qOFtkz4sVZtHX0iQIe9qY1KROdXdDFegdqBmm/R5 oXl3t1sbeIHB5F9uMhuoGwHWSnNl5Gvab59cZ1MV6geaNumLS1TTap/Xes1IutSlJN0q iRB/fKThby+KkmqjjHfifAyUU10P+n18NPvnSJN9Gr2VSLRDNWpc2i8W7XQQn0dMtw4+ paKiv76U5GAooDQMMWftVr8qX1O5jGkV9cXbsMW8h9PCZpusVm1RjWnqPzjaHCDOZbW6 d+gUSo+0QOSwIq3kYrtm2h0eszhqskkh/UYjxyLyQYVKnTbYr3vkLS4NDRZw942FI/aF x6CQ== Received: by 10.216.195.40 with SMTP id o40mr10060400wen.36.1345579281133; Tue, 21 Aug 2012 13:01:21 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.223.86.201 with HTTP; Tue, 21 Aug 2012 13:00:29 -0700 (PDT) In-Reply-To: <5033E0BC.8040507@ajf.me> References: <502EB667.1020602@lerdorf.com> <5033E0BC.8040507@ajf.me> Date: Wed, 22 Aug 2012 05:00:29 +0900 X-Google-Sender-Auth: CNFamM144ygXa87wUrqPNPQilBQ 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) 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. 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