Bug #75826 [Com]: Unintended array key integer to string conversion after unsetting in foreach
| From: | spam2 at rhsoft dot net | Date: | Tue, 16 Jan 2018 22:13:07 +0000 |
| Subject: | Bug #75826 [Com]: 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-213569@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
Comment by: spam2 at rhsoft dot net
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
Type: Bug
Package: JSON related
Operating System: Ubuntu
PHP Version: 7.2.1
Block user comment: N
Private report: N
New Comment:
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
)
Previous Comments:
------------------------------------------------------------------------
[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