|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #51915 numeric string key escaped in type change
Submitted: 2010-05-26 08:30 UTC Modified: 2011-02-09 09:23 UTC
Avg. Score:3.1 ± 1.2
Reproduced:5 of 7 (71.4%)
Same Version:1 (20.0%)
Same OS:3 (60.0%)
From: laruence at yahoo dot com dot cn Assigned:
Status: Not a bug Package: JSON related
PHP Version: 5.2.13 OS: linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: laruence at yahoo dot com dot cn
New email:
PHP Version: OS:


 [2010-05-26 08:30 UTC] laruence at yahoo dot com dot cn
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:
$data = array(
    123 => 'laruence',
    "03"=> 'baidu',

$value = json_encode($data);
$obj   = json_decode($value);
$arr   = (array)$obj;

Expected result:
array(2) {
  string(8) "laruence"
  string(5) "baidu"

Actual result:
array(2) {
  string(8) "laruence"
  string(5) "baidu"


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-05-27 04:15 UTC] laruence at yahoo dot com dot cn
-Type: Bug +Type: Feature/Change Request -Package: JSON related +Package: Scripting Engine problem
 [2010-05-27 04:15 UTC] laruence at yahoo dot com dot cn
change bug to Feature , nhancement
 [2010-06-07 10:03 UTC] xiezhenye at gmail dot com
there is an easier way to build such an array.

$obj = new stdClass;
$obj->{'123'} = 1;
$arr = (array) $obj;
 [2010-06-08 11:55 UTC]
-Package: Scripting Engine problem +Package: JSON related
 [2011-02-09 09:23 UTC]
-Status: Open +Status: Bogus
 [2011-02-09 09:23 UTC]
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 

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.
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Jun 19 19:01:32 2024 UTC