0

假设我有一个返回数组的函数/方法,我们称之为 ArrayReturner()。但我只想要第一个元素,[0]。现在我正在做类似的事情......

$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];

有没有一种方法可以在不需要临时 $arrayReturned 数组的情况下在一行中做到这一点?

4

2 回答 2

2

Depends on PHP's version you use.

If you're using PHP < 5.4, then you cannot get that, like ArrayReturner()[0]. That's only possible in PHP >= 5.4.

If you want your code to be portable, that would work with old and new versions, then you'd better stick with that code:

$arrayReturned = ArrayReturner();
$varIWant = $arrayReturned[0];
于 2013-10-22T00:54:44.177 回答
2

尝试:

$arrayReturned = reset(ArrayReturner());
于 2013-10-22T00:45:15.030 回答