|  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
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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Bug Type:
From: speller at yandex dot ru
New email:
PHP Version: OS:


 [2015-11-17 02:36 UTC] speller at yandex dot ru
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"

Test script:


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

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


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

Actual result:
NULL or Notice: Undefined offset: 1


Add a Patch

Pull Requests

Add a Pull Request


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.

"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:
 [2016-05-18 11:21 UTC]
-Status: Open +Status: Wont fix -Assigned To: +Assigned To: cmb
 [2016-05-18 11:21 UTC]
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.

 [2016-11-14 18:32 UTC]
-Status: Wont fix +Status: Closed
 [2016-11-14 18:32 UTC]
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

 For Windows:
Thank you for the report, and for helping us make PHP better.

Fixed by:
 [2016-11-14 18:36 UTC]
-Assigned To: cmb +Assigned To: ajf
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sat Apr 01 00:03:39 2023 UTC