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
  • How can there be a syntax error when there actually is no syntax? Commented Jun 7, 2011 at 23:16

7 Answers 7

5

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

4 Comments

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).
So how do you generalize it to access the $nth value from the array?
You could use array_slice, e.g. list($third)=array_slice(returnsAnArray(), 2, 1);
Ok this is valid to access known positioned items. What about to access known index from associated array getting returned from a function?
3

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 :)

1 Comment

this is one of the reason i dislike php. this should be a language builtin. (or at least let us do something like fReturnsArray()[1] ...
1

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

 <?php
 echo current(returnsAnArray());
 ?>

1 Comment

Nope, it won't. current() expects reference, therefore value cannot be passed. Returning & from returnsAnArray() may work, though.
1

Another option:

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

Similar thread: PHP: Can I reference a single member of an array that is returned by a function?

Comments

1

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.

Comments

0

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

foreach ( returnsAnArray() as $item )
  echo $item;

Comments

-1

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.

1 Comment

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.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.