tags:

views:

315

answers:

7
function returnsAnArray ()
{
  return array ('test');
}

echo returnsAnArray ()[0];

generates a syntax error in PHP. What's the most efficient way to directly obtain an element from a returned array without assigning the result to a temp variable?

+1  A: 

This will work if there is only one member in the array:

 <?php
 echo current(returnsAnArray());
 ?>
Eran Galperin
Michał Rudnicki
A: 

You could do this for an indexed array of unknown length.

foreach ( returnsAnArray() as $item )
  echo $item;
The Wicked Flea
+4  A: 

Here's one way, using the list language construct

function returnsAnArray ()
{
  return array ('test');
}

list($foo)=returnsAnArray();

You could grab a sequence of elements from an offset by combining this with array_slice

list($third,$fourth,$fifth)=array_slice(returnsAnArray(), 2, 3);
Paul Dixon
why the downvote? It's a valid way to access an array...
Paul Dixon
This is actually a very nice way to access an array. I've seen people stumble with it, though, maybe because it falls outside of the standard C-flavor syntax (i.e. something assigned to a function).
Nouveau
So how do you generalize it to access the `$n`th value from the array?
moritz
You could use array_slice, e.g. list($third)=array_slice(returnsAnArray(), 2, 1);
Paul Dixon
Ok this is valid to access known positioned items. What about to access known index from associated array getting returned from a function?
Shoaib
A: 

I ask myself why one would like to avoid creating a temporary variable for a returned array. Why don't you just return one value instead of an whole array? Maybe you'll have to overthink your program logic.

Or is it a performance/memory issue? Consider using references instead of always creating a new array object and returning it.

Markus Lux
The actual function is used to parse a resultset, in some specific cases I know I'll only be returning a single entry. I could refactor the solution to use a distinct function for single results, but felt I'd end up with either duplicate or convoluted code.
Jeroen van Delft
+2  A: 

Define a new function for returning a specific index from an array.

function arr_index($arr, $i) { return $arr[$i]; }

You might want to add some error and type checking there.

And then use it like this:

echo arr_index(returnsAnArray(), 0);

Happy Coding :)

pmg
+1  A: 

Another option:

<?php
echo reset(functionThatReturnsAnArray());
?>

Similar thread: http://stackoverflow.com/questions/68711/php-can-i-reference-a-single-member-of-an-array-that-is-returned-by-a-function

Darryl Hein
A: 

For regular numerically indexed arrays, where func() returns such an array and $n is the index you want:

array_pop(array_slice(func(),$n,1));

For associative arrays (e.g. strings or other things as keys), or numeric arrays that aren't numbered and complete from 0..n, it's a little more convoluted. Where $key is the key you want:

array_pop(array_intersect_keys(func(),Array($key => ""));

This would also work for the first case.