Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62353 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 29051 invoked from network); 21 Aug 2012 20:08:44 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 21 Aug 2012 20:08:44 -0000 Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; 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:56305] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 3F/C3-10139-BCAE3305 for ; Tue, 21 Aug 2012 16:08:44 -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 8BDADF0028; Tue, 21 Aug 2012 16:08:39 -0400 (EDT) Message-ID: <5033EAAB.6090502@ajf.me> Date: Tue, 21 Aug 2012 21:08:11 +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: <502EB667.1020602@lerdorf.com> <5033E0BC.8040507@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: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. 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] > > 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/