Edit report at http://bugs.php.net/bug.php?id=53750&edit=1
ID: 53750 Updated by: [email protected] Reported by: tuhin dot barua at gmail dot com Summary: dynamic variable name problem -Status: Open +Status: Bogus Type: Bug -Package: Arrays related +Package: Scripting Engine problem Operating System: ubuntu PHP Version: 5.2.17 Block user comment: N Private report: N New Comment: This is a simple operator associativity issue: $a->$arr[0] is actually parsed as $a->{$arr[0]}, and since the first character of $arr is "a", the last line in the test script ends up effectively being print_r($a->a). You can work around this by changing the last line of the script to: print_r($a->{$arr}[0]); Previous Comments: ------------------------------------------------------------------------ [2011-01-14 17:58:00] tuhin dot barua at gmail dot com Description: ------------ example 1: php > $a = new stdclass(); php > $a->arr = array(1,2,3); php > $arr = 'arr'; php > print_r($a->$arr); Array ( [0] => 1 [1] => 2 [2] => 3 ) php > print_r($a->$arr[0]); php > example 2: php > $arr = array(1,2,3); php > $a = 'arr'; php > print_r($$a); Array ( [0] => 1 [1] => 2 [2] => 3 ) php > print_r($$a[0]); arr php > print_r($$a[1]); php > Test script: --------------- $a = new stdclass(); $a->arr = array(1,2,3); $arr = 'arr'; print_r($a->$arr); print_r($a->$arr[0]) Expected result: ---------------- cant access array elements with a dynamic variable name. ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/bug.php?id=53750&edit=1
