Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62267 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 23000 invoked from network); 19 Aug 2012 23:06:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 19 Aug 2012 23:06:37 -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:62514] helo=mail-we0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id DF/F8-03087-C7171305 for ; Sun, 19 Aug 2012 19:06:36 -0400 Received: by weyr1 with SMTP id r1so4172388wey.29 for ; Sun, 19 Aug 2012 16:06:33 -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=BU/CfxrEFCQOWUPrnFpvdB6wGuBurhwYOvbyb+LfV4c=; b=jHQGSEeNAVipkWchjN8pIqk9fUP9obs9QWhC+1Ld4Y/SsY6pVBmWbQ0nXmVE6p974D 5+iioksmij9xPv1ojP96mzbYc6ktDDCf7FmLMdJa/9gRwMIBVFf7Tz7M0KJXmP4uANca qz70RRRCW2r3qsl0CNdiE4OLnezpA4OnLxvcSOa2XcuwKhWbrZfpxtOochWg1GLet1Ml IlxMiYMHu3ZNf3rVOtcD32mYBht1metclm/kD2OyR6ejSqtpoPaVtlwMAnXzF68YmVnM Gw+2DxQDQGPW9OasNU6TTu+/DXmLzstd576tyHNVQonlYaM+thW/SyR8P1VX8OSd61cA pxpw== Received: by 10.180.74.33 with SMTP id q1mr22825765wiv.4.1345417593232; Sun, 19 Aug 2012 16:06:33 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.223.86.201 with HTTP; Sun, 19 Aug 2012 16:05:51 -0700 (PDT) In-Reply-To: References: <502EB667.1020602@lerdorf.com> Date: Mon, 20 Aug 2012 08:05:51 +0900 X-Google-Sender-Auth: Sv8znOVTc3rCZdE7gi_pAmQuC_s Message-ID: To: Etienne Kneuss Cc: 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 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. -- Yasuo Ohgaki yohgaki@ohgaki.net