PHP: Array access short?

In Javascript, after the function is executed, I can immediately get the array element returned by the function, for example:

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

This is much simpler and faster than doing this:

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

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

+5
source share
3 answers

No, unfortunately, in PHP you can only index an array variable, no other type of return expression.

+4
source
reset(myFunc("birds"))

Will work well, although it does not work with associative arrays.

Or, of course, you could write a function, for example.

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

echo arr_get(myFunc("birds"), 0);
+1
source

All Articles