Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:62248 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 22860 invoked from network); 18 Aug 2012 22:19:07 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 18 Aug 2012 22:19:07 -0000 Authentication-Results: pb1.pair.com smtp.mail=ajf@ajf.me; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ajf@ajf.me; 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:39080] helo=oxmail.registrar-servers.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 01/53-00850-AD410305 for ; Sat, 18 Aug 2012 18:19:07 -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 2B618F002C; Sat, 18 Aug 2012 18:19:02 -0400 (EDT) Message-ID: <503014BD.8010007@ajf.me> Date: Sat, 18 Aug 2012 23:18:37 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:14.0) Gecko/20120714 Thunderbird/14.0 MIME-Version: 1.0 To: Rasmus Lerdorf CC: Rasmus Schultz , internals@lists.php.net References: <502EB667.1020602@lerdorf.com> <502EBA6E.9020603@lerdorf.com> In-Reply-To: <502EBA6E.9020603@lerdorf.com> 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 17/08/12 22:41, Rasmus Lerdorf wrote: > On 08/17/2012 05:35 PM, Rasmus Schultz wrote: >> Most other languages have more than one collection-type... since PHP has >> only the single, hybrid array-type which acts both as a hash and as an >> array, something like this ought to be available. >> >> I don't know why everyone is so eager to jump up and argue against >> something this simple, basic and useful. The fact that this is missing >> is an oversight - just look at the number of solutions people come up >> with. For what? >> >> I want to remove and object from a list. Not an exotic and crazy >> requirement, is it? It just isn't the sort of thing that should require >> any substantial thinking or typing. >> >> And it doesn't help make codebases more legible when people come up with >> 25 different ways to do it. > The problem is that it is a rather unusual thing to do. I don't mean > removing an element, that is very common, but you are implying that you > know you don't have duplicates and you want a fast way to remove an > element by value in that case. To me that means you built your array > badly. If the values are unique then they should be the keys, or at > least a representation of the value should be the key such that you > don't need to scan the entire hash for the element when you need to > access it. And removal is just like any other access. > > -Rasmus > > Hmm. I can think of a particular instance I've needed to remove an item from an array by value, and where keys wouldn't be an option. In PHP you can only have string or int keys, you can't have object keys. In Python, I've used Twisted (an excellent asynchronous OOP networking framework) to write client-server applications. One thing I did was maintain a list of clients (that is, a Python list). When a client disconnected, I would do list.remove(client). Of course there might be better ways to implement this, but it was simple and worked. Anything wrong with allowing the same in PHP? -- Andrew Faulds http://ajf.me/