Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I use an array index on the return value of a function? [closed]

Why can't I do this?

explode(',','1,2,3', 1)[0]

Every other language supports it.

Answers I'm looking for: (since people seem to think this is a pointless rant)

Is there some sort of a difference between how a variable and a return value behaves that I should be made aware of?

Was this a design decision? Were they just lazy? Is there something about the way the language was built that makes this exceedingly difficult to implement?

like image 558
mpen Avatar asked Jun 02 '11 15:06

mpen


People also ask

Why is my index match function not working?

If you believe that the data is present in the spreadsheet, but MATCH is unable to locate it, it may be because: The cell has unexpected characters or hidden spaces. The cell may not be formatted as a correct data type. For example, the cell has numerical values, but it may be formatted as Text.

Can index function return an array?

If array has more than one row and more than one column, and only row_num or column_num is used, INDEX returns an array of the entire row or column in array.

What happens if you attempt to access an array index that is outside of the length of an array?

What will happen if you attempt to retrieve an array element which is outside the array's upper bound? This means that if you try to access an element outside of the bounds of the array, you will get a run-time error instead of accessing random memory as in unsafe languages.

How do you return the index of an element in an array?

To find the position of an element in an array, you use the indexOf() method. This method returns the index of the first occurrence the element that you want to find, or -1 if the element is not found.


3 Answers

Why can't I do this?

Because PHP doesn't currently support this syntax. But you can pass it to a function, e.g.:

current(explode(',','1,2,3', 1));

Otherwise, you have to assign the return value to a variable first, then access via index.

More Info:

So one can chain method calls or property access. Now for a long time people requested the same thing for array offset access. This was often rejected due to uncertainties about memory issues, as we don't like memory leaks. But after proper evaluation Felipe committed the patch which allows you to do things like:

function foo() {
    return array(1, 2, 3);
}
echo foo()[2]; // prints 3

http://schlueters.de/blog/archives/138-Features-in-PHP-trunk-Array-dereferencing.html

So, it is in the development pipeline but, as I stated, is not currently supported.

Update

PHP >= 5.4 now supports function array dereferencing, e.g. foo()[0]

like image 177
webbiedave Avatar answered Oct 21 '22 18:10

webbiedave


In my framework I wrote a function to be able to do it with all possiible array:

function elem($array,$key) {
  return $array[$key];
}

//> usage 
echo elem($whateverArrayHere,'key');
echo elem(explode(),1);
like image 25
dynamic Avatar answered Oct 21 '22 19:10

dynamic


Explode return an array, not reference.

$tab = explode (',','1,2,3', 1);
$tab[0] = ','
like image 1
red eyes dev Avatar answered Oct 21 '22 18:10

red eyes dev