Re: why cant I: array_keys($arr)[0] ?
| From: | Matt McClanahan | Date: | Sun, 04 Nov 2001 10:37:43 +0000 |
| Subject: | Re: why cant I: array_keys($arr)[0] ? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-73386@lists.php.net to get a copy of this message | ||
On Sun, Nov 04, 2001 at 12:54:54AM -0700, operator@superprivate.com wrote:
> Why does PHP give a parse error if you do:
>
> echo array_keys($arr)[0];
>
> It makes you assign the result of the function to a var first like this:
>
> $arr = array_keys($arr);
> echo $arr[0];
>
> I just want to grab the 1st element of the array. Why does it make you do
> it in 2 lines instead of letting you index right on the array that results
> from the function?
// Shifts the first element off the array
$first_element = array_shift($array);
// Returns the first element, preserving the array in the calling scope
function get_array_first($arr)
{
return array_shift($arr);
}
$first_element = get_arr_first($array);
Matt