php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #69601 Miss array key access after json_decode
Submitted: 2015-05-08 10:07 UTC Modified: 2015-05-08 11:18 UTC
From: turin dot tomsk at gmail dot com Assigned: cmb (profile)
Status: Not a bug Package: JSON related
PHP Version: 5.5.24 OS: Linux
Private report: No CVE-ID: None
 [2015-05-08 10:07 UTC] turin dot tomsk at gmail dot com
Description:
------------
When json_encode array and json_decode it, can't get access to array key of object and generate PHP Notice.

Test script:
---------------
<?php
$a = [1 => 2];
$s = json_encode($a);
$b = @json_decode($s);
$q = (array)$b;
var_dump($q[1]); // PHP Notice:  Undefined offset: 1 in php shell code on line 1
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-05-08 11:18 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2015-05-08 11:18 UTC] cmb@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Read the section on "Converting to array" in the manual[1]. What
you actually want to do is

  $q = json_decode($s, true);

[1] <http://php.net/manual/en/language.types.array.php#language.types.array.casting>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Apr 16 09:01:28 2024 UTC