开发者

php: accessing a value from a returned associative array

开发者 https://www.devze.com 2023-01-24 01:13 出处:网络
I have a function getStatus() that returns an associative array. Right now I\'m accessing a value with two commands:

I have a function getStatus() that returns an associative array.

Right now I'm accessing a value with two commands:

$a = $user->getStatus();
$a = $a['keyName'];
doSomething($a);

Is there a way to rephrase that into one command like:

do开发者_高级运维Something($user->getStatus()['keyName']);


No, unfortunately that doesn't work.

However, if the order of the returned elements is fixed, you could write something like

list($a) = array_values($user->getStatus());

Or you could write a function that returns an array value:

$a = my_array_value($user->getStatus(),'keyName');
0

精彩评论

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

关注公众号