7

In php how can I access an array's values without using square brackets around the key? My particular problem is that I want to access the elements of an array returned by a function. Say function(args) returns an array. Why is $var = function(args)[0]; yelling at me about the square brackets? Can I do something like $var = function(args).value(0); or am I missing something very basic?

amb
  • 489
  • 5
  • 9
  • php refers to this as function array referencing. It has been included in php since version 5.4 http://php.net/manual/en/migration54.new-features.php – Steve Nov 13 '15 at 15:56

5 Answers5

10

As the others have said, you pretty much have to use a temporary variable:

$temp = myFunction();
$value = $temp[0];

But, if know the structure of the array being returned it is possible to avoid the temporary variable.

If you just want the first member:

$value = reset(myFunction());

If you want the last member:

$value = end(myFunction());

If you want any one in between:

// second member
list(, $value) = myFunction();

// third
list(, , $value) = myFunction();

// or if you want more than one:

list(, , $thirdVar, , $fifth) = myFunction();
nickf
  • 537,072
  • 198
  • 649
  • 721
  • Hey, clever! `list()` never ceases to amaze me. +1. – Pekka Mar 23 '10 at 01:29
  • 1
    reset() and end() require the arguments be references. You get an E_STRICT notice in recent versions of php. – goat Mar 23 '10 at 01:43
  • 1
    yes, i have been doing this with temporary variables, but was wondering whether i really needed to. now i'm just wondering _why_ i have to. but in any case your list() usage is pretty clever. thanks! – amb Mar 23 '10 at 05:27
2

In PHP, when getting an array as a function result, you unfortunately have to do an extra step:

$temp_array = function($args);
$var = $temp_array[0];

For objects, this has been relaxed in PHP 5. You can do:

$echo function($args)->property;

(provided function returns an object of course.)

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1
function getKey($array, $key){
    return $array[$key];
}

$var = getKey(myFunc(args), $key);

There is no way to do this without adding a user function unfortunately. It is just not part of the syntax.

You could always just do it the old fashion way

$array = myFunc();
$value = $array[0];
Tyler Carter
  • 60,743
  • 20
  • 130
  • 150
1

What exactly matches your expecting is:

echo pos(array_slice($a=myFunc(), pos(array_keys(array_keys($a), 'NameOfKey'));

answered Kinetix Kin, Taipei

1

if you want this, its probably best to be returning an object (unfortunately, its totally lame php doesnt support this). Heres a crazy way i was able to figure out though, out of novelty (please dont do this!):

function returnsArray(){
    return array("foo" => "bar");
}

echo json_decode(json_encode((object)returnsArray()))->foo;
//prints 'bar'

So yeah..until they add support for array dereferencing in php, i think you should probably just cast the return array as an object:

return (object)array("foo" => "bar");

and then you can do returnsArray()->foo, since php relaxes dereferencing for objects but not arrays.. or of course write a wrapper function like others have suggested.

mutexkid
  • 11
  • 2
  • also, looks like support for this has been recently added to php? http://wiki.php.net/rfc/functionarraydereferencing – mutexkid Nov 17 '10 at 20:18