php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70925 json_decode-d object become inacccessible after converting to array
Submitted: 2015-11-17 02:36 UTC Modified: 2016-11-14 18:36 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: speller at yandex dot ru Assigned: ajf (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.6.15 OS:
Private report: No CVE-ID: None
 [2015-11-17 02:36 UTC] speller at yandex dot ru
Description:
------------
Bug converting json_decoded-d object to array. First seen in PHP 5.4. Confirmed in PHP 5.6.15.

See the test script. 

We have a simple array $a. Next we convert it to JSON and back. But after transcoding we can not access the array's values and get NULL or "Notice: Undefined offset: 1".

If replace the line "$a = (array)json_decode(json_encode($a));" with "$a = (array)((object)$a);" everything is OK, converted array's values are accessible.

The test script output is:

array(2) {
  [1] =>
  string(7) "value 1"
  [2] =>
  string(7) "value 2"
}
string(7) "value 1"
string(7) "value 1"

array(2) {
  '1' =>
  string(7) "value 1"
  '2' =>
  string(7) "value 2"
}
NULL
NULL

Test script:
---------------
<?

error_reporting(0);

$a = ['1' => 'value 1', '2' => 'value 2'];
var_dump($a);
var_dump($a[1]);
var_dump($a['1']);

$a = (array)json_decode(json_encode($a));

var_dump($a);
var_dump($a[1]);
var_dump($a['1']);
?>

Expected result:
----------------
The values of the converted array must be accessible

Actual result:
--------------
NULL or Notice: Undefined offset: 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-11-17 03:01 UTC] nhojohl at gmail dot com
This is a known issue and was marked as "won't fix" long ago. It's in the official PHP documentation as well.

http://www.php.net/manual/en/language.types.array.php#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"

The recommended solution is to use the second parameter of json_decode to have the son decoded as an array without the need to cast it.

For more information see: https://bugs.php.net/bug.php?id=45959
 [2016-05-18 11:21 UTC] cmb@php.net
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2016-05-18 11:21 UTC] cmb@php.net
Won't fix according to bug #45959.

> The recommended solution is to use the second parameter of
> json_decode to have the son decoded as an array without the need
> to cast it.

Exactly.
 [2016-11-14 18:32 UTC] ajf@php.net
-Status: Wont fix +Status: Closed
 [2016-11-14 18:32 UTC] ajf@php.net
The fix for this bug has been committed.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.

 For Windows:

http://windows.php.net/snapshots/
 
Thank you for the report, and for helping us make PHP better.

Fixed by: https://wiki.php.net/rfc/convert_numeric_keys_in_object_array_casts
 [2016-11-14 18:36 UTC] ajf@php.net
-Assigned To: cmb +Assigned To: ajf
 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC