Possible duplicate:PHP syntax for dereference function result
function returnArray(){ $array = array( 0 => "kittens", 1 => "puppies" ); return $array; } echo returnArray()[0];
How can I do this without assigning an entire array to a variable?
Why don't you pass a parameter in your function?
function returnArray($key=null){ $array = array( 0 => "kittens", 1 => "puppies" ); return is_null($key) ? $array : $array[$key]; } echo returnArray(0); // only 0 key echo returnArray(); // all the array
This is proposed, but not yet available.
http://wiki.php.net/rfc/functionarraydereferencing
We'll see
No error testing
function returnArray($i){ static $array = array( 0 => "kittens", 1 => "puppies" ); return $array[$i]; } echo returnArray(0);
There is no way to do this without any temporary variable.
ps: this is an example of a "god-like" function. The function should not return more than you need.