Bug #70925 [NEW]: json_decode-d object become inacccessible after converting to array
| From: | speller at yandex dot ru | Date: | Tue, 17 Nov 2015 02:36:46 +0000 |
| Subject: | Bug #70925 [NEW]: json_decode-d object become inacccessible after converting to array | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-197290@lists.php.net to get a copy of this message | ||
From: speller at yandex dot ru
Operating system:
PHP version: 5.6.15
Package: Scripting Engine problem
Bug Type: Bug
Bug description:json_decode-d object become inacccessible after converting to array
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
--
Edit bug report at https://bugs.php.net/bug.php?id=70925&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70925&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70925&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70925&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70925&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70925&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70925&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70925&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70925&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70925&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70925&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70925&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70925&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70925&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70925&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70925&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70925&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70925&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70925&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70925&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70925&r=mysqlcfg