Bug #74859 [Opn->Dup]: json_decode unexpected behaviour
| From: | requinix@php.net | Date: | Thu, 06 Jul 2017 15:27:19 +0000 |
| Subject: | Bug #74859 [Opn->Dup]: json_decode unexpected behaviour | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-209860@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=74859&edit=1
ID: 74859
Updated by: requinix@php.net
Reported by: emimarz at gmail dot com
Summary: json_decode unexpected behaviour
-Status: Open
+Status: Duplicate
Type: Bug
Package: JSON related
Operating System: linux
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
Then this is a(nother) duplicate of a bug that has been fixed with 7.2.
https://3v4l.org/CS6BJ
In PHP 5.x/7.0/7.1, don't cast objects with numeric property names to arrays. Really, avoid
creating those sorts of objects in the first place if you can.
For JSON decode straight to an array if you want an array, or in the general case use
get_object_vars,
https://3v4l.org/QcicS
or if you like to hurt yourself then you can use advanced syntax on the object.
https://3v4l.org/ZARaP
Previous Comments:
------------------------------------------------------------------------
[2017-07-06 14:57:05] emimarz at gmail dot com
this two return NULL
var_dump($json['1']); // incorrect result
var_dump($json[1]); // incorrect result
I trying to access an element existent but return NULL
the expected return is something that is not NULL because that element exists
------------------------------------------------------------------------
[2017-07-05 18:33:40] peehaa@php.net
What unexpected behavior?
Please explain what is expected and why and what the actual result is.
------------------------------------------------------------------------
[2017-07-05 18:24:36] emimarz at gmail dot com
Description:
------------
this unexpected behaviour happen on php 5.* and 7.1 with json_decode
Test script:
---------------
<?php
$son = '{
"1":{ "id":"1", "name":"jack" },
"2":{ "id":"2", "name":"john" }
}';
$json = (array) json_decode($son);
//var_dump($json);
var_dump($json['1']); // incorrect result
var_dump($json[1]); // incorrect result
$json = json_decode($son,true);
var_dump($json['1']); // correct result
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=74859&edit=1