Alternative to temporary variable to access array-result from function in PHP [duplicate]
Possible Duplicate:
PHP syntax for dereferencing function result
If a PHP function returns an array, the following syntax will not work:
$firstValue = $object-&开发者_StackOverflowgt;methodThatReturnsArray()[0]; // syntax error, unexpected '['
This, however, works fine:
$temporaryArray = $object->methodThatReturnsArray();
$firstValue = $temporaryArray[0]; // temporary will never be reused
What is the best syntax to solve this problem, or is creating that variable the recommended approach?
Variable is the best approach.
Still PHP 5.4 adds the feature to be able using the first mentioned syntax.
Well, you can have list
to the left of the =
(list($firstValue) = $object->methodThatReturnsArray();
), if you need anything too deep in the array, a temporary variable is your only option.
精彩评论