简体   繁体   中英

Define array index after function call

In other languages such as C# and JavaScript, I am able to access the index of an array with a function call such as

getMyArray()[0] 

This would allow me to access the first index of the result instead of passing back the entire array and then setting the result.

However this shortcut does not work with PHP. Is there a way to get this shortcut?

您需要运行PHP 5.4才能使用数组取消引用

// PHP 5.4
$item = getMyArray()[0];

// Older than 5.4: (not recommended)
list($a)   = getMyArray();  // getMyArray()[0]
list(, $b) = getMyArray();  // getMyArray()[1]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM