Re: Indexing an array

From: Date: Mon, 09 Aug 2010 11:47:03 +0000
Subject: Re: Indexing an array
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-49225@lists.php.net to get a copy of this message
On 08/06/2010 04:44 PM, Richard Quadling wrote:
On 6 August 2010 15:33, mathieu.suen<mathieu.suen@easyflirt.com> wrote:
Hi, For now you can only index an array using a scalar type or a string. Is there some rfc or work going on to enlarge the possibility so that it is possible to have some other object like: - closure - object - etc. Thanks -- Mathieu Suen
     
If an object implements a __toString() magic method, then it can work ...
<?php class randomizer { public function __toString() { return (string)mt_rand(); } } $randomizer = new randomizer; $array = array ( "$randomizer" => 'First', "$randomizer" => 'Second', "$randomizer" => 'Third', "$randomizer" => 'Fourth', ); print_r($array); ?> outputs ... Array (
     [1365443950] =>  First
     [1235256771] =>  Second
     [520059180] =>  Third
     [486985268] =>  Fourth
) Well that is not the expected behavior since if you call array_keys you won't get the object. --Mathieu Suen

Thread (11 messages)

« previous php.internals (#49225) next »