3

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
  • 3
    Possible duplicate of php access array value from function return. In other words: No, that's not possible with the current PHP syntax. Unfortunately, there's no especially concise way to use an element of an array a function returns. Commented Nov 7, 2010 at 0:28

1 Answer 1

5

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');
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.