Req #51915 [Opn->Bgs]: numeric string key escaped in type change
| From: | scottmac@php.net | Date: | Wed, 09 Feb 2011 08:23:24 +0000 |
| Subject: | Req #51915 [Opn->Bgs]: numeric string key escaped in type change | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-158626@lists.php.net to get a copy of this message | ||
Edit report at http://bugs.php.net/bug.php?id=51915&edit=1
ID: 51915
Updated by: scottmac@php.net
Reported by: laruence at yahoo dot com dot cn
Summary: numeric string key escaped in type change
-Status: Open
+Status: Bogus
Type: Feature/Change Request
Package: JSON related
Operating System: linux
PHP Version: 5.2.13
Block user comment: N
Private report: N
New Comment:
When an array is of mixed type or doesn't contain sequential keys it has
to be
treated as a hash and not as an integer
json_encode() turns the integer keys into strings which it has to do to
be valid
json.
On json_decode() it see's string keys instead of numeric since this is a
hash
not an array.
There is nothing unexpected happening here.
Previous Comments:
------------------------------------------------------------------------
[2010-06-07 10:03:41] xiezhenye at gmail dot com
there is an easier way to build such an array.
$obj = new stdClass;
$obj->{'123'} = 1;
$arr = (array) $obj;
var_dump($obj);
------------------------------------------------------------------------
[2010-05-27 04:15:01] laruence at yahoo dot com dot cn
change bug to Feature , nhancement
------------------------------------------------------------------------
[2010-05-26 08:30:18] laruence at yahoo dot com dot cn
Description:
------------
php treat numeric string key as interge,
bug sometimes there is some exception.
for example:
while change a std object to array, numeric string key doesn't cast to
number
Test script:
---------------
<?php
$data = array(
123 => 'laruence',
"03"=> 'baidu',
);
$value = json_encode($data);
$obj = json_decode($value);
$arr = (array)$obj;
var_dump($arr);
?>
Expected result:
----------------
array(2) {
[123]=>
string(8) "laruence"
["03"]=>
string(5) "baidu"
}
Actual result:
--------------
array(2) {
["123"]=>
string(8) "laruence"
["03"]=>
string(5) "baidu"
}
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/bug.php?id=51915&edit=1