Map array on function return - PHP

1

Testing location (xampp), I get on the return of the function mapping an array. Example:

<?php $res = $obj->Function()[0]['name']; ?>

And on the production server (typical hosting) is an interpolated form has an error of type "Parse Error ";

  

PHP Parse error: syntax error, unexpected '[' in path / archive.php on line 10

How to enable the availability of working with this syntax?

    
asked by anonymous 21.06.2016 / 03:32

2 answers

1

This feature you want is called array dereferencing , and is available in PHP 5.4 upwards. According to PHP documentation :

  

As of PHP 5.4 it is possible to array dereference the result of a function or method call directly. Before it was only possible using a temporary variable.

That means, something you used to do in PHP 5.3 down:

$array = ['a', 'b', 'c'];
echo $array[0]; // a

... can be simplified in PHP 5.4 up:

echo ['a', 'b', 'c'][0]; // 1
    
21.06.2016 / 09:36
0

Try this

<?php $res = $obj->Function();
 echo $res[0]['name'];
?>

You will get the return and instantiate in your variable $res ai you can manipulate as you want

    
21.06.2016 / 06:23