Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62275 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40338 invoked from network); 20 Aug 2012 01:33:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Aug 2012 01:33:07 -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.170 as permitted sender) X-PHP-List-Original-Sender: yohgaki@gmail.com X-Host-Fingerprint: 209.85.212.170 mail-wi0-f170.google.com Received: from [209.85.212.170] ([209.85.212.170:40446] helo=mail-wi0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 00/7C-03087-1D391305 for ; Sun, 19 Aug 2012 21:33:06 -0400 Received: by wibhq12 with SMTP id hq12so2819808wib.5 for ; Sun, 19 Aug 2012 18:33:02 -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=1uDTXWHad4jOK1yVT3iHqZVpOK3fhybX4XqjLIxJG+0=; b=gvNwrcuEG2FxAlW1CEMVTyRSkpgBTXksgT0K2YWQWJXNeAw1naFGW9bhuXdKwjP688 svgGSm29nd+jnBl4H6ztWjh+vv0Yz0OY4YOO3G2XdWGcmHmp0G2yR6T69l93d8vpBZ8Y ElRhEr/p7mOxUnYQigPf5mSDMTUk7dG8jnlXQL208GIIigqsIuzI4uQTgAxyGsR9W02G TLmZ38EeX02XCO70PlNa7NvAY43yNrLBBPo3fFWkbdVLS418CZOv6pAmvQssukm+sI1G 0d5MvlOhxJd+MCA1nXsIb2fCHtZSmVCR+Pat/Hf7+DWgxk2me3FtobPAKaEXWu1Qku/7 CZiA== Received: by 10.216.192.85 with SMTP id h63mr6301676wen.7.1345426382638; Sun, 19 Aug 2012 18:33:02 -0700 (PDT) MIME-Version: 1.0 Sender: yohgaki@gmail.com Received: by 10.223.86.201 with HTTP; Sun, 19 Aug 2012 18:32:21 -0700 (PDT) In-Reply-To: <50317466.2050101@ajf.me> References: <502EB667.1020602@lerdorf.com> <503171D5.6060906@ajf.me> <50317466.2050101@ajf.me> Date: Mon, 20 Aug 2012 10:32:21 +0900 X-Google-Sender-Auth: BvIK33XKDuHuWBQLgNsvTUXtZhw 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: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. Of course it works. $array = array_filter($array, function($v) use ($del_val) { return ($v !== $del_val); }); It does not delete element(s), but creates new array. void array_delete ( array &$input [, callable $callback = "" | $values_to_be_deleted ] ) This will delete element(s). We can use array_walk() to delete element(s) from array also. php > $array = array(1,2,3,4,5); php > array_walk($array, function($val, $key) use (&$array) {if ($val == 3) unset($array[$key]);}); php > var_dump($array); array(4) { [0]=> int(1) [1]=> int(2) [3]=> int(4) [4]=> int(5) } I suppose this isn't an intuitive way for novices because stack overflow's answers didn't have this method that actually delete element(s) from an array. Adding an intuitive element deletion function is the point of this thread, isn't it? I'll vote +1 for adding array_delete(). Regards, -- Yasuo Ohgaki > >>>>>> 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/ >