开发者

Alternative to temporary variable to access array-result from function in PHP [duplicate]

开发者 https://www.devze.com 2023-03-16 23:39 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: PHP syntax for dereferencing function result
This question already has answers here: Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号