function getArray() { return array(1, 2, 3); } // в PHP 5.4 $secondElement = getArray()[1]; // ранее делали так $tmp = getArray(); $secondElement = $tmp[1]; // или так list(, $secondElement) = getArray(); 
  • Just convenient and faster access to the element of the result of the function without creating intermediate variables - Alexey Shimansky
  • @ AlekseyShimansky I already thought that this was some kind of abstruse method of deleting keys or something like that. - user208916

1 answer 1

This code is from the PHP documentation. Previously, it was impossible to write

 $secondElement = getArray()[1]; 

I had to first assign the result of the function to a variable and already work with the variable.

By the way, if an element of an array is an object, then you can

 $secondElementName = getArray()[1]->name; 

And you need this for convenience, fewer lines of code, and the code itself is clearer.