php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #75826 Unintended array key integer to string conversion after unsetting in foreach
Submitted: 2018-01-16 22:06 UTC Modified: 2018-01-17 04:18 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: leon dot valkenborg at blackpepper dot co dot nz Assigned:
Status: Not a bug Package: JSON related
PHP Version: 7.2.1 OS: Ubuntu
Private report: No CVE-ID: None
 [2018-01-16 22:06 UTC] 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"}

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2018-01-16 22:13 UTC] 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:24 UTC] leon dot valkenborg at blackpepper dot co dot nz
-Status: Open +Status: Closed
 [2018-01-16 22:24 UTC] leon dot valkenborg at blackpepper dot co dot nz
This is normal behavior for non-sequential arrays.

https://stackoverflow.com/questions/2118339/how-to-encode-to-json-encoded-array-from-non-sequential-array
 [2018-01-17 04:18 UTC] requinix@php.net
-Status: Closed +Status: Not a bug
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 18:01:27 2024 UTC