Re: Indexing an array
| From: | Richard Quadling | Date: | Fri, 06 Aug 2010 14:44:23 +0000 |
| Subject: | Re: Indexing an array | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-49215@lists.php.net to get a copy of this message | ||
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
)