Re: why cant I: array_keys($arr)[0] ?

From: Date: Sun, 04 Nov 2001 08:11:35 +0000
Subject: Re: why cant I: array_keys($arr)[0] ?
References: 1  Groups: php.general php.general 
Request: Send a blank email to php-general+get-73380@lists.php.net to get a copy of this message
operator@superprivate.com wrote:
Why does PHP give a parse error if you do:
      echo array_keys($arr)[0];
This breaks syntax...
It makes you assign the result of the function to a var first like this:
      $arr = array_keys($arr);
      echo $arr[0];
How about reset($arr); list($key,) = each($arr); echo $key; It's much more efficient.
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?
Writing 2 or 3 lines for printing first key name, is not hard work... PHP script may need more lines to do the same thing compare to Perl, but it's much easier to maintain. IMHO. -- Yasuo Ohgaki

« previous php.general (#73380) next »