#45959 [Opn]: Object to array conversion leads to weird behaviour
| From: | kalle@php.net | Date: | Wed, 05 Nov 2008 07:53:57 +0000 |
| Subject: | #45959 [Opn]: Object to array conversion leads to weird behaviour | ||
| References: | 1 | Groups: | php.doc.bugs |
| Request: | Send a blank email to doc-bugs+get-1334@lists.php.net to get a copy of this message | ||
ID: 45959
Updated by: kalle@php.net
Reported By: manchokapitancho at gmail dot com
Status: Open
Bug Type: Documentation problem
Operating System: windows xp
PHP Version: 5.2.6
New Comment:
You could always use the 'assoc' parameter on the json_decode()
function to avoid this completely:
$input =
json_decode('{"is_paid":{"0":"4","1":"3"},"is_bank_transfer":{"4":"1"}}',
true);
$arr = $input['is_bank_transfer'];
var_dump($arr);
var_dump($arr[4]);
var_dump($arr['4']);
$keys = array_keys ($arr);
var_dump($keys);
var_dump($arr[$keys[0]]);
:)
Previous Comments:
------------------------------------------------------------------------
[2008-11-04 08:18:08] manchokapitancho at gmail dot com
Isn't this problem quite a serious issue since JSON data transfer
mechanism is growing in popularity and manipulating a JavaScript (or
JSON) key/value object as a PHP array is a common use case?
------------------------------------------------------------------------
[2008-11-03 21:56:41] felipe@php.net
Fixing that implies a perfomance decrease, hence the better seems be
keep it as an known issue, but documented.
------------------------------------------------------------------------
[2008-08-31 13:53:10] manchokapitancho at gmail dot com
Also array_key_exists returns false instead of true.
------------------------------------------------------------------------
[2008-08-31 13:52:29] manchokapitancho at gmail dot com
Description:
------------
Object to array conversion leads to weird behaviour - access to array
members is not possible.
Reproduce code:
---------------
$input = json_decode
('{"is_paid":{"0":"4","1":"3"},"is_bank_transfer":{"4":"1"}}');
$arr = ((array)$input->is_bank_transfer);
var_dump($arr);
var_dump($arr[4]);
var_dump($arr['4']);
$keys = array_keys ($arr);
var_dump($keys);
var_dump($arr[$keys[0]]);
Expected result:
----------------
array(1) {
["4"]=>
string(1) "1"
}
string(1) "1"
string(1) "1"
array(1) {
[0]=>
string(1) "4"
}
string(1) "1"
Actual result:
--------------
array(1) {
["4"]=>
string(1) "1"
}
NULL
NULL
array(1) {
[0]=>
string(1) "4"
}
NULL
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=45959&edit=1