#51015 [NEW]: Array keys can not be used to get values
| From: | nat at search dot ch | Date: | Thu, 11 Feb 2010 13:57:10 +0000 |
| Subject: | #51015 [NEW]: Array keys can not be used to get values | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-147438@lists.php.net to get a copy of this message | ||
From: nat at search dot ch
Operating system: Linux #1 SMP 2009-08-15 17:53:59
PHP version: 5.3.1
PHP Bug Type: *General Issues
Bug description: Array keys can not be used to get values
Description:
------------
In an associative array with number as text keys, the keys can not be used
to get the values. "Foreach" works.
var_dump($choices);
// array(2) { ["1"]=> array(2) { [0]=> string(1) "0" [1]=> string(1)
"1" } ["0"]=> array(2) { [0]=> string(1) "0" [1]=>
string(1) "1" } }
var_dump($choices['1']); // NULL
var_dump($choices[1]); // NULL
var_dump($choices["1"]); // NULL
var_dump($choices->{1}); // NULL
var_dump($choices->{'1'});//NULL
var_dump($choices['0']); //NULL
var_dump($choices[0]); //NULL
foreach ($choices as $k => $v) {
var_dump($k, $v);
}
// string(1) "1" array(2) { [0]=> string(1) "0" [1]=> string(1)
"1" }
// string(1) "0" array(2) { [0]=> string(1) "0" [1]=> string(1)
"1" }
Reproduce code:
---------------
$db_data_model='{"events":{"1":"film","0":"avatar"},"times":{"0":"2010-10-09
08:00","2":"2010-10-09
11:00","3":"2010-09-09"},"choices":{"1":["0","1"],"0":["0","1"]}}';
$model = json_decode($db_data_model);
$choices = (array)$model->choices;
var_dump($choices);
var_dump($choices['1']); // NULL
var_dump($choices[1]); // NULL
var_dump($choices->{1}); // NULL
var_dump($choices->{'1'});//NULL
foreach ($choices as $k => $v) {
var_dump($k, $v);
}
Expected result:
----------------
var_dump($choices['1']);
// array(2) { [0]=> string(1) "0" [1]=> string(1) "1" }
var_dump($choices['0']);
// array(2) { [0]=> string(1) "0" [1]=> string(1) "1" }
Actual result:
--------------
var_dump($choices['1']);
// NULL
var_dump($choices['0']);
// NULL
--
Edit bug report at http://bugs.php.net/?id=51015&edit=1
--
Try a snapshot (PHP 5.2): http://bugs.php.net/fix.php?id=51015&r=trysnapshot52
Try a snapshot (PHP 5.3): http://bugs.php.net/fix.php?id=51015&r=trysnapshot53
Try a snapshot (PHP 6.0): http://bugs.php.net/fix.php?id=51015&r=trysnapshot60
Fixed in SVN: http://bugs.php.net/fix.php?id=51015&r=fixed
Fixed in SVN and need be documented: http://bugs.php.net/fix.php?id=51015&r=needdocs
Fixed in release: http://bugs.php.net/fix.php?id=51015&r=alreadyfixed
Need backtrace: http://bugs.php.net/fix.php?id=51015&r=needtrace
Need Reproduce Script: http://bugs.php.net/fix.php?id=51015&r=needscript
Try newer version: http://bugs.php.net/fix.php?id=51015&r=oldversion
Not developer issue: http://bugs.php.net/fix.php?id=51015&r=support
Expected behavior: http://bugs.php.net/fix.php?id=51015&r=notwrong
Not enough info: http://bugs.php.net/fix.php?id=51015&r=notenoughinfo
Submitted twice: http://bugs.php.net/fix.php?id=51015&r=submittedtwice
register_globals: http://bugs.php.net/fix.php?id=51015&r=globals
PHP 4 support discontinued: http://bugs.php.net/fix.php?id=51015&r=php4
Daylight Savings: http://bugs.php.net/fix.php?id=51015&r=dst
IIS Stability: http://bugs.php.net/fix.php?id=51015&r=isapi
Install GNU Sed: http://bugs.php.net/fix.php?id=51015&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=51015&r=float
No Zend Extensions: http://bugs.php.net/fix.php?id=51015&r=nozend
MySQL Configuration Error: http://bugs.php.net/fix.php?id=51015&r=mysqlcfg