Php: access value from returned associative array

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

Now I am accessing a value with two commands:

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

Is there a way to rephrase this into a single command, for example:

doSomething($user->getStatus()['keyName']);
+3
source share
1 answer

No, unfortunately, this does not work.

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

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

Or you can write a function that returns the value of an array:

$a = my_array_value($user->getStatus(),'keyName');
+6
source

Source: https://habr.com/ru/post/1773443/


All Articles