Bug #75826 [Opn->Csd]: Unintended array key integer to string conversion after unsetting in foreach
| From: | leon dot valkenborg at blackpepper dot co dot nz | Date: | Tue, 16 Jan 2018 22:24:18 +0000 |
| Subject: | Bug #75826 [Opn->Csd]: Unintended array key integer to string conversion after unsetting in foreach | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-213570@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=75826&edit=1
ID: 75826
User updated by: leon dot valkenborg at blackpepper dot co dot nz
Reported by: leon dot valkenborg at blackpepper dot co dot nz
Summary: Unintended array key integer to string conversion
after unsetting in foreach
-Status: Open
+Status: Closed
Type: Bug
Package: JSON related
Operating System: Ubuntu
PHP Version: 7.2.1
Block user comment: N
Private report: N
New Comment:
This is normal behavior for non-sequential arrays.
https://stackoverflow.com/questions/2118339/how-to-encode-to-json-encoded-array-from-non-sequential-array
Previous Comments:
------------------------------------------------------------------------
[2018-01-16 22:13:04] spam2 at rhsoft dot net
the bug is more that it don't behave that way before the unset because there is nothing like
arrays without kyes in PHP
php > $x = ['a', 'b', 'c'];
php > print_r($x);
Array
(
[0] => a
[1] => b
[2] => c
)
------------------------------------------------------------------------
[2018-01-16 22:06:28] leon dot valkenborg at blackpepper dot co dot nz
Description:
------------
json_encode will parse the PHP array as if it has string keys when a numerical key is unset.
var_dump() shows expected results. So i assume this issue is wit json_encode().
Test script:
---------------
<?
$array = array('a','b','c');
unset($array[0]);
echo json_encode($array);
?>
Expected result:
----------------
["b","c"]
Actual result:
--------------
{"1":"b","2":"c"}
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=75826&edit=1