Bug #77038 [NEW]: Object to Array conversion gives duplicate keys
| From: | jonathan at x6 dot nl | Date: | Fri, 19 Oct 2018 07:32:22 +0000 |
| Subject: | Bug #77038 [NEW]: Object to Array conversion gives duplicate keys | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-217632@lists.php.net to get a copy of this message | ||
From: jonathan at x6 dot nl
Operating system: CentOS Linux release 7.5.1804
PHP version: 7.0.32
Package: *General Issues
Bug Type: Bug
Bug description:Object to Array conversion gives duplicate keys
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.
--
Edit bug report at https://bugs.php.net/bug.php?id=77038&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=77038&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=77038&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=77038&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=77038&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=77038&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=77038&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=77038&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=77038&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=77038&r=support
Expected behavior: https://bugs.php.net/fix.php?id=77038&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=77038&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=77038&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=77038&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=77038&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=77038&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=77038&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=77038&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=77038&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=77038&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=77038&r=mysqlcfg