Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62270 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 27109 invoked from network); 19 Aug 2012 23:16:59 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Aug 2012 23:16:59 -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 209.85.212.176 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.212.176 mail-wi0-f176.google.com Received: from [209.85.212.176] ([209.85.212.176:45401] helo=mail-wi0-f176.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 5D/E9-03087-AE371305 for ; Sun, 19 Aug 2012 19:16:58 -0400 Received: by wibhn17 with SMTP id hn17so2730045wib.11 for ; Sun, 19 Aug 2012 16:16:55 -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=rDv0vbb+2HGtMdbT3z6jNqQsX7xyP0lGkr21msUxelc=; b=xHzmApzdPSgEa1F3HoSKYwJ4UHCqHs4M0UIGn0E2fgIjWy19CbcZfF8pgoj+7gATkU 9V1ewxRM2a7EET2ppS7KEblrtHDuaUt1r/Ty5gBzJH7zFt6gX261zCEervExYH1eKViV vnyrwuTOS9OKnB3ZEaHMDRAjzMIbpmoPvII2+0eluco6MnyGqipmG0XaFmP7Vzju3Efp HjSCyFyU4uvdW1/KB64tuFIYPZDq3ilxGTA9gJondeGMuJ4U01t8FXjjaeq4605rrmtg mSdq153H3WKpKLM1idp2KAO17p326LwECvtJjqwH6ECNXb1ptbxFQbICqfETMfz4fbox BUOQ== Received: by 10.216.195.40 with SMTP id o40mr6544339wen.36.1345418215221; Sun, 19 Aug 2012 16:16:55 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.223.86.201 with HTTP; Sun, 19 Aug 2012 16:16:14 -0700 (PDT) In-Reply-To: <503171D5.6060906@ajf.me> References: <502EB667.1020602@lerdorf.com> <503171D5.6060906@ajf.me> Date: Mon, 20 Aug 2012 08:16:14 +0900 X-Google-Sender-Auth: WTeezg7FhYTDcP0QGszQe7lKzCI Message-ID: To: Andrew Faulds Cc: Etienne Kneuss , Rasmus Lerdorf , 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/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. >>>> 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