Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:63260 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 70663 invoked from network); 4 Oct 2012 16:48:57 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 4 Oct 2012 16:48:57 -0000 Authentication-Results: pb1.pair.com smtp.mail=adamjonr@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=adamjonr@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.219.42 as permitted sender) X-PHP-List-Original-Sender: adamjonr@gmail.com X-Host-Fingerprint: 209.85.219.42 mail-oa0-f42.google.com Received: from [209.85.219.42] ([209.85.219.42:56828] helo=mail-oa0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 45/F1-46522-8FDBD605 for ; Thu, 04 Oct 2012 12:48:56 -0400 Received: by mail-oa0-f42.google.com with SMTP id j1so773221oag.29 for ; Thu, 04 Oct 2012 09:48:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:date:message-id:subject:from:to:content-type; bh=5sFc0mK9Hue87Nn/IdL5pUikWuoJpXTxZDlQ2j3EaT4=; b=BcidwEdhsWNm9F/ntopQdRcwBiz1jafIFYyIp9qLDVXibzg6YYnLdAGbaX0E9bd0cZ +SbeXMIU5BFrsdSrrQOIpjW/6ige5mFEczfrnZFBwbhAPrs0EvfMA0KIQf4bDJwsCdhg cPMj+p78XF7WS91TMrKTlkqlfoL+sOBfBWo5HXC1L0fUxRhGcOpMUMi7VuuQZ8iIMwBW ftGr8kKzGuYHE8+MjbBVtCr4tl4cKYK4UekSXYn2IQxrnk+E8vlA6fRs6zuLaflo1+50 Yx/IANpxa1bML0ERihQ3ja5V+NX5poPGYGT252LQ26LI9QpFV636/swUzUX1704uuEUU GLbg== MIME-Version: 1.0 Received: by 10.182.47.66 with SMTP id b2mr4779728obn.34.1349369333965; Thu, 04 Oct 2012 09:48:53 -0700 (PDT) Received: by 10.76.95.198 with HTTP; Thu, 4 Oct 2012 09:48:53 -0700 (PDT) Date: Thu, 4 Oct 2012 12:48:53 -0400 Message-ID: To: internals@lists.php.net Content-Type: text/plain; charset=ISO-8859-1 Subject: Arrays which have properties of sets From: adamjonr@gmail.com (Adam Jon Richardson) A while back, there was a thread discussing adding a specific function for removing elements from an array by value. Rasmus L. noted that when the values are unique, they would be better represented as keys: > 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. When I come across situations where an array's properties match those of a set, I create array "sets" by duplicating keys and values: array("val1" => "val1", "val2" => "val2"); This approach has several benefits: - Form the union of values using the "+" operator (which forms unions through use of keys) - Fast, easy removal of values through unset (e.g., unset($elements["val1"])) - Standard handling of values in for loops: for ($elements as $element) { echo $element; } Writing a userland function to create array sets from numerically-indexed arrays (avoiding the duplication of typing the key and value) is indeed trivial: function create_set($arr) { return array_combine($arr, $arr); } $set = create_set(array("val1","val2")); However, I find that the use of array sets is very common and helpful to me, and I see many situations where a developer would have been better served by leveraging the key to store the value. I suspect many PHP users don't understand the nature of PHP arrays. I'm wondering if adding a function (or language construct) for creating array sets would prove helpful (just because of the vast amount of code which could make use of it) and facilitate the education of developers so they might better leverage the benefits of the underlying hash map structure when their array is essentially a set and they wish to perform operations such as removing an item by value. Adam