解析返回数组从一个函数调用
问题描述:
我喜欢在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 printed');
}
?>
寻找一个短语法的第一回波将打印这将让打印
第二类和第三类语法错误
在PHP中有这样的事情吗?
答
PHP在当前版本中不支持此功能。对于下一个即将发布的PHP版本,计划包含此功能。
查看php.net wiki(本rfc及相关指标)或the blog post of the php 5.3 release manager。
现在一两件事的工作原理是:
<?php
function x() { return array(1,2,3); }
list($one, $two,) = x();
等。也许其他人会发布更多的例子,但一般workarround看起来不错。我不知道。
答
不,PHP不支持。 (据戈登它已经在后备箱里。)
但你可以写一个小功能:
function arrayOffset($array, $index) {
return $array[$index];
}
arrayOffset(func(), 2);
PS:其实我在prephp这一功能的实现,但我不认为它应该被使用。这可能是越野车,我还没有开发该项目很长一段时间(虽然我希望恢复)。 (Test,Implementation)
答
这在PHP 5.4中支持,而不是在PHP 5.3中支持。
[PHP:获得数组元素]的可能重复(http://stackoverflow.com/questions/3484832/php-get-array-element) – VolkerK 2010-08-25 10:24:22
[数组解除引用将很快来到你身边的PHP](http: //schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html) – Gordon 2010-08-25 10:27:06