Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm not sure if this is possible, but I can't figure out how to do it if it is...

I want to get a specific element out of an array that is returned by a function, without first passing it into an array... like this...

$item = getSomeArray()[1];


function getSomeArray(){
     $ret = Array();
     $ret[0] = 0;
     $ret[1] = 100;
     return $ret;
}

What's the correct syntax for this?

share|improve this question
1  
possible duplicate of Access array returned by a function in php – outis Jan 19 '12 at 10:52

6 Answers

up vote 7 down vote accepted

PHP cannot do this yet, it's a well-known limitation. You have every right to be wondering "are you kidding me?". Sorry.

If you don't mind generating an E_STRICT warning you can always pull out the first and last elements of an array with reset and end respectively -- this means that you can also pull out any element since array_slice can arrange for the one you want to remain first or last, but that's perhaps taking things too far.

But despair not: the (at this time upcoming) PHP 5.4 release will include exactly this feature (under the name array dereferencing).

Update: I just thought of another technique which would work. There's probably good reason that I 've never used this or seen it used, but technically it does the job.

// To pull out Nth item of array:
list($item) = array_slice(getSomeArray(), N - 1, 1);
share|improve this answer
wow... how bizarre... ok... no worries... I'll pass it through a temp holder array. Thanks very much. – Genia S. Jan 17 '12 at 1:45

Yes, php can't do that. Bat you can use ArrayObect, like so:

$item = getSomeArray()->{1};
// Credits for curly braces for Bracketworks    

function getSomeArray(){
     $ret = Array();
     $ret[0] = 0;
     $ret[1] = 100;
     return new ArrayObject($ret, ArrayObject::ARRAY_AS_PROPS);
}

Okay, maybe not working with numeric keys, but i'm not sure.

share|improve this answer
2  
That'll raise a fatal; you need to do getSomeArray()->{1}; Use curly braces for any non-compliant property names; see SimpleXML's basic usage for related information. – Bracketworks Jan 17 '12 at 1:50

As the others says, there is no direct way to do this, I usually just assign it to a variable like so:

$items = getSomeArray();
$item = $items[1];


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}
share|improve this answer

I am fairly certain that is not possible to do in PHP. You have to assign the returned array to another array before you can access the elements within or use it directly in some other function that will iterate though the returned array (i.e. var_dump(getSomeArray()) ).

share|improve this answer

The syntax you're referring to is known as function array dereferencing. It's not yet implemented and there's an RFC for it.

The generally accepted syntax is to assign the return value of the function to an array and access the value from that with the $array[1]syntax.

That said, however you could also pass the key you want as an argument to your function.

function getSomeArray( $key = null ) {
  $array = array(
     0 => "cheddar",
     1 => "wensleydale"
  );

  return is_null($key) ? $array : isset( $array[$key] ) ? $array[$key] : false;    
}

echo getSomeArray(0); // only key 0
echo getSomeArray(); // entire array
share|improve this answer

I know this is an old question, but, in your example, you could also use array_pop() to get the last element of the array, (or array_shift() to get the first).

<?php
$item = array_pop(getSomeArray());


function getSomeArray(){
    $ret = Array();
    $ret[0] = 0;
    $ret[1] = 100;
    return $ret;
}
share|improve this answer
thanks... that wasn't at all my intention, though I can see with an array of 2 items how it can be misconstrued as what I'm looking for. I was trying to figure out how to get array elements directly returned by a function. I have since abandoned PHP as the giant steaming turd that it is and have not looked back. Thanks though! :) – Genia S. Apr 17 at 5:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.