Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php: accessing a value from a returned associative array

Tags:

php

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:

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

1 Answers

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');
like image 162
AndreKR Avatar answered Feb 17 '26 17:02

AndreKR



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!