|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2003-04-02 10:30 UTC] mfischer@php.net
Supported dereferencing array indeces on arbitrary expressions.
Currently you can only dereference array indeces on variables directly:
$a = array('a', 'b');
var_dump($a[1]);
but this does not work
var_dump(array('a', 'b')[1]);
neither does this:
function returnArray() {
return array('a', 'b');
}
var_dump(returnArray()[1]);
or this:
$value = ($ns == 'foo' ? $thisArray : $thatArray)[0];
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Tue Oct 28 09:00:01 2025 UTC |
doesn't fix the bug, but a workaround: function array_val($array, $key) { return $array[$key]; } then you can do things like: var_dump(array_val(array('a', 'b'), 1));Developer guys, we php programmers indeed need this function to direct access element of the function/method returned array. Sometimes i work around this so: echo ${!${''}=&explode(',','a,b,c')}[1]; maybe it's the shortest hack code to mimic direct access, but it's ugly, we really need to realize it in the php build-in syntax supporting.Guys, we really need this feature to make our code beautiful. Now: function get2nd(){ $tempArray = explode(',', array('a','b','c')); return $tempArray[1]; } Better: return explode(',', array('a','b','c'))[1]; or: return array_get(explode(',', array('a','b','c')), '1'); Thanks.When could we realize this in php? create_function('$v', <<<'fun_code' echo $v; fun_code ')('hello');When could we realize this in php? create_function('$v', <<<'fun_code' echo $v; fun_code )('hello');For objects, you could hack like this: class user {public $username='jack'; public function __get($prop_name) {return $this->$prop_name(); } private function degree() {return array('doctor', 'master'); } } $aUser=new user(); echo $aUser->degree[1];