1

I have a function that returns an array of named values ['upper','lower','chosen']. To read the upper value, I do this

$returned = myfunction($input1, $input2);
$upper = $returned['upper'];

Is there any way to get it all done in 1 line, maybe like this, but this syntax doesn't work. Any other way?

$upper = myfunction($input1, $input2)['upper'];
sameold
  • 18,400
  • 21
  • 63
  • 87

3 Answers3

4

You can do something like:

 list ($upper, $lower, $chosen) = myfunction($input1, $input2);
zkhr
  • 739
  • 3
  • 7
3

Your function could return an object instead of an array. Then you could do something like this :

class myObject {
    public $upper;
    public $lower;
    public $chosen;
}

$upper = myFunction($input1,$input2)->upper;
flatzo
  • 133
  • 1
  • 9
2

If upper is always the first element, you can use array_shift():

$returned = array_shift(myfunction($input1, $input2));
karim79
  • 339,989
  • 67
  • 413
  • 406