|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2010-02-12 16:45 UTC] jani@php.net
[2010-02-12 18:48 UTC] nat at search dot ch
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Dec 04 17:00:01 2025 UTC |
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