Closed misq007 closed 4 years ago
For incremental numeric keys (0, 1, 2 ...) in PHP is interpreted as a JSON array.
Incremental, started from zero, string type keys:
$ php -r 'var_dump(json_encode(["0" => "a","1" => "b"]));'
string(9) "["a","b"]"
not incremental:
$ php -r 'var_dump(json_encode([1 => "a",0 => "b"]));'
string(17) "{"1":"a","0":"b"}"
$ php -r 'var_dump(json_encode(["0" => "a","2" => "b"]));'
string(17) "{"0":"a","2":"b"}"
Please see that I cast it to object in purpose:
$data = (object)$data;
and note that json_encode works fine.
In my case, it was a filter that returns only a special group with ID 0. But without filter - it returns 0 and 13 which is fine.
The Yii Json helper should not break the behavior of json_encode
because people do not expect it and it causing hard to find bugs.
I think this should be fixed.
Please read and test my example and the proposed solution.
Duplicate of https://github.com/yiisoft/yii2/issues/16621
What steps will reproduce the problem?
I have a custom API that is using
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON
and returns a list with groups where the group_id was a key. Note that in my case the 0 was the ID of a group and only one group was returned.What is the expected result?
{"0":"test"}
What do you get instead?
["test"]
json_encode($data);
works fine. The problem occurs because you foreach it and convert it to array here: https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseJson.php#L171I think it should check if:
Additional info