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?
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.
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 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.
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.
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.
PHP >= 5.4 now supports function array dereferencing, e.g. foo()[0]
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);
Explode return an array, not reference.
$tab = explode (',','1,2,3', 1);
$tab[0] = ','
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With