#51015 [Opn->Bgs]: Array keys can not be used to get values
ID: 51015
Updated by: jani@php.net
Reported By: nat at search dot ch
-Status: Open
+Status: Bogus
Bug Type: *General Issues
Operating System: Linux #1 SMP 2009-08-15 17:53:59
PHP Version: 5.3.1
New Comment:
RTFM:
http://php.net/array#language.types.array.casting
"If an object is converted to an array, the result is an array whose
elements are the object's properties. The keys are the member variable
names, with a few notable exceptions: integer properties are
unaccessible.."
And you could also access those directly without casting anyway, like
this:
var_dump($model->choices->{1});
Previous Comments:
------------------------------------------------------------------------
[2010-02-11 13:57:10] nat at search dot ch
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 this bug report at http://bugs.php.net/?id=51015&edit=1
Thread (3 messages)