PHP数组语法分析错误左方括号“;[quot;

PHP数组语法分析错误左方括号“;[quot;,php,arrays,function,reference,Php,Arrays,Function,Reference,我有一个返回数组的函数。我有另一个只返回第一行的函数,但出于某种原因,它使我使用了一个中间变量,即此函数失败: function f1(/*some args*/) { return /*an array*/; } function f2(/*some args*/) { return f1(/*some args*/)[0]; } …与: 分析错误:语法错误,第10行util.php中出现意外的“[” 但是,这是可行的: function f1(/*some args*/)

我有一个返回数组的函数。我有另一个只返回第一行的函数,但出于某种原因,它使我使用了一个中间变量,即此函数失败:

function f1(/*some args*/) {
    return /*an array*/;
}
function f2(/*some args*/) {
    return f1(/*some args*/)[0];
}
…与:

分析错误:语法错误,第10行util.php中出现意外的“[”

但是,这是可行的:

function f1(/*some args*/) {
    return /*an array*/;
}
function f2(/*some args*/) {
    $temp = f1(/*some args*/);
    return $temp[0];
}

我在网上找不到任何相关的东西(我的搜索结果总是被人们用“?”、“{”、“来混淆)。你不能使用函数数组去引用

return f1(/*some args*/)[0];

此行为的原因是PHP函数不能像JavaScript函数那样链接。就像
document.getElementsByTagNames('a')[0]
一样

对于PHP版本<5.4,您必须坚持第二种方法

已添加函数数组解引用,例如foo()[0]


语言的可能重复直到5.4.0才允许它,即使这已经很旧了,我想我还是要留下一条评论。如果您使用
current
,您可以返回索引0,如下所示:
返回current(f1(/*一些参数*/)
。为了揭示正确的术语,它结束了对本应是一个简单问题的长期搜索。