ID: 23022
Comment by: jim at akubo dot net
Reported By: [email protected]
Status: Open
Bug Type: Feature/Change Request
Operating System: Any
PHP Version: 5CVS-2003-04-02 (dev)
New Comment:
i wish you could use the * operator (like in c++) to derference
references, is there a way todo this in php? instead of writing a new
function like array_val
Previous Comments:
------------------------------------------------------------------------
[2008-11-25 04:27:37] showmethemoney at google dot com
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.
------------------------------------------------------------------------
[2008-11-25 03:28:59] kexianbin at diyism dot com
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.
------------------------------------------------------------------------
[2007-02-23 22:15:16] m_rayman at bigfoot dot com
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));
------------------------------------------------------------------------
[2003-04-02 10:30:35] [email protected]
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];
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=23022&edit=1