Re: Indexing an array
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
)
Thread (11 messages)