parsing returned array from a function call
I'm searching for a short syntax like in PERL
<?php
# instead of this ( PHP )
$f = returnArray();
echo $f[2];
unset($f);
# just do this ( PERL short code )
echo (returnArray())[2];
# or this ( PERL short code )
echo returnArray()->[2];
function returnArray()
{
return array(0=>'x', 1=>'y', 2=>'This will get开发者_JAVA百科 printed');
}
?>
The first echo will print This will get printed
The second and third are syntax errorsThere is a way of doing something like this in PHP ?
PHP does not support this in it's current version. For the next upcoming release of php there plans to include this feature.
See the php.net wiki (this rfc and related ones) or the blog post of the php 5.3 release manager.
For now one thing that works is:
<?php
function x() { return array(1,2,3); }
list($one, $two, ) = x();
and so on. Maybe other people will post more examples but a general workarround looking as nice.. i don't know of.
No, PHP doesn't support this yet. (According to Gordon it is already in the trunk.)
But you may write a small function:
function arrayOffset($array, $index) {
return $array[$index];
}
arrayOffset(func(), 2);
PS: Actually I have an implementation of this feature in prephp, but I don't think it shall be used. It may be buggy and I haven't developed the project for quite some time (though I hope to resume). (Test, Implementation)
This is supported in PHP 5.4, and not in PHP 5.3.
精彩评论