5

In Javascript, after executing a function I can immediately get the an element of the array returned by the function, like so:

myFunc("birds")[0] //gets element zero returned from "myFunc()"

This is much easier and faster than doing this:

$myArray = myFunc("birds");
echo $myArray[0];

Does PHP have a similar shorthand to javascript? I'm just curious. Thanks in advance!

4

3 回答 3

4

No, unfortunately in PHP you can only subscript an array variable, no other kind of array returning expression.

于 2010-03-21T00:01:26.253 回答
1
reset(myFunc("birds"))

Will work nicely, although it doesn't work on associative arrays.

Or, of course, you could write a function, e.g.

function arr_get($arr, $i)
{
 return $arr[$i];
}

echo arr_get(myFunc("birds"), 0);
于 2010-12-17T23:51:44.050 回答
0

Short answer is no.

Longer answer is maybe coming soon. See:

Last time I used the PHP trunk, it worked fine.

于 2011-02-27T01:18:58.983 回答