Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62271 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 28437 invoked from network); 19 Aug 2012 23:19:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Aug 2012 23:19:33 -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.133 as permitted sender) X-PHP-List-Original-Sender: ajf@ajf.me X-Host-Fingerprint: 64.22.89.133 oxmail.registrar-servers.com Received: from [64.22.89.133] ([64.22.89.133:34626] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DD/3A-03087-48471305 for ; Sun, 19 Aug 2012 19:19:32 -0400 Received: from [192.168.0.200] (5ad4bfa0.bb.sky.com [90.212.191.160]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by oxmail.registrar-servers.com (Postfix) with ESMTPSA id B074E75803D; Sun, 19 Aug 2012 19:19:28 -0400 (EDT) Message-ID: <50317466.2050101@ajf.me> Date: Mon, 20 Aug 2012 00:19:02 +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: Etienne Kneuss , Rasmus Lerdorf , Rasmus Schultz , internals@lists.php.net References: <502EB667.1020602@lerdorf.com> <503171D5.6060906@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 20/08/12 00:16, Yasuo Ohgaki wrote: > 2012/8/20 Andrew Faulds : >> On 20/08/12 00:05, Yasuo Ohgaki wrote: >>> 2012/8/20 Etienne Kneuss : >>>> On Sun, Aug 19, 2012 at 11:57 PM, Yasuo Ohgaki >>>> wrote: >>>>> 2012/8/18 Rasmus Lerdorf : >>>>>> On 08/17/2012 05:21 PM, Rasmus Schultz wrote: >>>>>>>> if(($key = array_search($del_val, $messages)) !== false) { >>>>>>>> unset($messages[$key]); >>>>>>>> } >>>>>>>> >>>>>>>> Nothing horrible here. >>>>>>>> >>>>>>> I disagree - this is (or should be) a simple, atomic operation... >>>>>>> yet, you've got a function-call, an intermediary variable, a boolean >>>>>>> test, >>>>>>> and an unset statement repeating the name of the array you're deleting >>>>>>> from. >>>>>>> >>>>>>> This should be a simple statement or function/method-call, and in most >>>>>>> other languages it would be... >>>>>> Really? I can't think of a single language that has a call to remove an >>>>>> element by value in a key-value hash. Do you have some examples? What >>>>>> do >>>>>> you do with duplicates? >>>>>> >>>>> Ruby can do (using irb) >>>>> >>>>> ruby-1.9.2-p180 :007 > h = {"apple"=>150, "banana"=>300, "lemon"=>300} >>>>> => {"apple"=>150, "banana"=>300, "lemon"=>300} >>>>> ruby-1.9.2-p180 :008 > h.delete_if { |k,v| v==300 } >>>>> => {"apple"=>150} >>>>> >>>>> May be we should have something like >>>>> >>>>> array_delete_if($array, function($v, $k=null) { if ($v == 300) return >>>>> true; }) >>>> So array_filter? >>> I'll use it or like for deleting, but the point of this thread is >>> "intuitive function for deleting element(s)" >>> >>> array_delete($array, $value|callable) >>> >>> would be nicer for users, perhaps. >> A callable? Wouldn't that mean you couldn't delete strings? :( > If you read my previous post, you'll see why it would be nicer with callable. Being unable to remove callables from an array would be very strange, and I think for the use case you presented array_filter works fine. >>>>> Ruby can do (using irb) >>>>> >>>>> ruby-1.9.2-p180 :007 > h = {"apple"=>150, "banana"=>300, "lemon"=>300} >>>>> => {"apple"=>150, "banana"=>300, "lemon"=>300} >>>>> ruby-1.9.2-p180 :008 > h.delete_if { |k,v| v==300 } >>>>> => {"apple"=>150} >>>>> >>>>> May be we should have something like >>>>> >>>>> array_delete_if($array, function($v, $k=null) { if ($v == 300) return >>>>> true; }) > -- > Yasuo Ohgaki -- Andrew Faulds http://ajf.me/