|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #23022 Supported dereferencing array indeces on arbitrary expressions
Submitted: 2003-04-02 10:30 UTC Modified: 2012-10-06 13:03 UTC
Avg. Score:5.0 ± 0.1
Reproduced:2196 of 2196 (100.0%)
Same Version:2167 (98.7%)
Same OS:2181 (99.3%)
From: Assigned: cataphract (profile)
Status: Closed Package: *General Issues
PHP Version: 5CVS-2003-04-02 (dev) OS: Any
Private report: No CVE-ID: None
 [2003-04-02 10:30 UTC]
Supported dereferencing array indeces on arbitrary expressions.

Currently you can only dereference array indeces on variables directly:

$a = array('a', 'b');

but this does not work

var_dump(array('a', 'b')[1]);

neither does this:

function returnArray() {
    return array('a', 'b');

or this:

$value = ($ns == 'foo' ? $thisArray : $thatArray)[0];


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2007-02-23 22:15 UTC] 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));
 [2008-11-25 03:28 UTC] 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.
 [2008-11-25 04:27 UTC] showmethemoney at google dot com
Guys, we really need this feature to make our code beautiful.

function get2nd(){
    $tempArray = explode(',', array('a','b','c'));
    return $tempArray[1];
return explode(',', array('a','b','c'))[1];
return array_get(explode(',', array('a','b','c')), '1');

 [2009-01-29 01:32 UTC] jim at akubo dot net
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
 [2009-02-09 03:13 UTC] Anatorian at gmail dot com
I think this feature is very important. It can make me happier in php developing.
 [2009-02-26 08:20 UTC] kexianbin at diyism dot com
When could we realize this in php?

create_function('$v', <<<'fun_code'
echo $v;
 [2009-02-26 08:44 UTC] kexianbin at diyism dot com
When could we realize this in php?

create_function('$v', <<<'fun_code'
echo $v;
 [2009-11-05 05:43 UTC] glennwidener at gmail dot com
I found this lack of symmetry in array references really lame too.  If an expression delivers an array, then you should be able to reference an element of that array.  The only question is operator precedence and grouping: recommend following the C rules - primary expressions group left to right, parenthesis disambiguate.
 [2009-11-10 03:47 UTC] kexianbin at diyism dot com
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];
 [2009-12-31 06:26 UTC] billyellison99 at gmail dot com
I fully support this feature. It'd make my life a whole lot easier, and 
make my code much neater and smaller.
 [2010-02-12 21:37 UTC] zeb at zebhodge dot com
I'm baffled that this doesn't work in PHP like it does in so many other 
 [2010-02-13 12:43 UTC]
Closing, since for better or worse, this has since gone through the RFC process and been declined. Cite:
 [2010-10-27 17:11 UTC]
-Status: Bogus +Status: Closed -Package: Feature/Change Request +Package: *General Issues -Assigned To: +Assigned To: cataphract
 [2010-10-27 17:11 UTC]
Marking as closed, as this has been implemented in trunk.
 [2010-10-27 17:14 UTC]
It appears marking as closed assigned it to me; this was actually implemented by Felipe Pena, see
 [2012-10-06 12:44 UTC] hovik dot melikyan at gmail dot com
Can we please reopen this?

This hasn't been implemented in full, as array()[] and [][] still don't work.

It just boggles my mind why on Earth array dereferencing shouldn't be symmetric 
and applicable to arbitrary subexpressions, like in (virtually) any other 
programming language.
 [2012-10-06 13:03 UTC]
Such feature will be added in the next PHP (> 5.4) version. (i.e. currently it's implemented in the master branch)
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Tue Feb 07 19:05:51 2023 UTC