Jump to content

Syntax for function result array as variable


domwalton

Recommended Posts

I have a large amount of legacy code to tidy up. There are lots of functions that return arrays, when only one item is needed. I don't really wish to go through and reverse engineer all the functions so rather then do this each time:

 

$somevar = somefunction();

$iwantthis = $somevar['thisone'];

 

It was be very useful to do something like:

 

$iwantthis = somefunction()['thisone'];

 

I just can't recall the correct syntax for this, or whether it is actually allowed in PHP?

As the function is returning an array, I think you will have to stick with picking the array element you want, after the function returns.

 

I do not think you can assign a variable to one element of the returned array on the return.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.