Re: PHP array unions

From: Date: Fri, 14 Sep 2012 07:11:16 +0000
Subject: Re: PHP array unions
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-319080@lists.php.net to get a copy of this message
On Fri, Sep 14, 2012 at 2:30 AM, Matijn Woudt <tijnema@gmail.com> wrote: > > It doesn't need to be clunky.. just use array_flip and you've got the old > array again.. > Well, array_flip has it's own potential issues (duplicate values are lost, so my example of using zeros would not work.) I suppose I could duplicate the keys as the values (e.g., array('value 1' => 'value 1', 'value 2' => 'value 2', ...).) Then, the keys would allow me to utilize the nice properties of hash maps whilst maintaining the ability to work with the values as one normally does in typical array functions. Ex: $a1 = array('apples' => 'apples', 'oranges' => 'oranges', 'pears' => 'pears'); $a2 = array('oranges' => 'oranges', 'kiwi' => 'kiwi'); // can use the union operator without any additional calls and the performance is stellar $a3 = $a1 + $a2 // can use the values of the array using the convention that the value is what you expect to handle/manipulate foreach ($a3 as $val) { echo $val } Here, the clunkiness is the redundancy in the array, but, as Claude Shannon has demonstrated, redundancy isn't all bad :) Adam -- Nephtali: A simple, flexible, fast, and security-focused PHP framework http://nephtaliproject.com

« previous php.general (#319080) next »