|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2018-10-19 08:26 UTC] nikic@php.net
-Status: Open
+Status: Duplicate
[2018-10-19 08:26 UTC] nikic@php.net
[2018-10-19 08:28 UTC] paul dot crovella at gmail dot com
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Nov 06 23:00:02 2025 UTC |
Description: ------------ In the case if you have a stdClass with only numeric keys (for example, this is a decoded json string). And convert it to array, it will change the keys to strings. If you then add that same value as an integer into the array, it will be added to the array. If you then do a json_encode, you get an invalid json string, because you have duplicate keys. It seems somehow that when the object is converted to array, the types of values will be changed. This seems like a bug, since in our particular example we tried to check if an ID was already in the array (with doing isset($array[$id])). This example returned false because the ID was changed to a string. Test script: --------------- <?php // Create new stdClass with numeric attribute, stored as string. $bla = new stdClass; $bla->{"1012"} = "Value 1"; $bla->{1012} = "Value 2"; // Convert to array $bla = (array) $bla; if (!isset($bla[1012])) { // ISSET fails, because it is now "1012" instead of 1012 var_dump($bla); $bla[1012] = "Value 4"; } var_dump($bla); echo json_encode($bla); Expected result: ---------------- The object needs to be correctly converted.