#51015 [Bgs]: Array keys can not be used to get values
| From: | nat at search dot ch | Date: | Fri, 12 Feb 2010 18:48:18 +0000 |
| Subject: | #51015 [Bgs]: Array keys can not be used to get values | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-147502@lists.php.net to get a copy of this message | ||
ID: 51015
User updated by: nat at search dot ch
Reported By: nat at search dot ch
Status: Bogus
Bug Type: *General Issues
Operating System: Linux #1 SMP 2009-08-15 17:53:59
PHP Version: 5.3.1
New Comment:
$choices is a variable of a new kind. It is no:
- Boolean
- Integer
- Floating point number
- String
- Array
- Object
It is an inconsequence with the rest of php.
I think this case should be considered in future versions.
Previous Comments:
------------------------------------------------------------------------
[2010-02-12 16:45:44] jani@php.net
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});
------------------------------------------------------------------------
[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