note 125324 rejected from function.json-encode by cmb
| From: | cmb@php.net | Date: | Fri, 04 Sep 2020 06:55:43 +0000 |
| Subject: | note 125324 rejected from function.json-encode by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-215069@lists.php.net to get a copy of this message | ||
Note Submitter: ruben_e at yahoo dot com
----
Currently I'm using PHP 7.4
If i decode a json string with two objects inside:
Array
(
[0] => stdClass Object
(
[userId] => 6
[userName] => Ruben Elizondo
[subscribedOn] => 2020-09-03 16:10:04
)
[1] => stdClass Object
(
[userId] => 1
[userName] => Ruben Elizondo updated again
[subscribedOn] => 2020-09-03 16:10:43
)
)
if i json_encode them i get:
[{"userId":6,"userName":"Ruben
Elizondo","subscribedOn":"2020-09-03
16:10:04"},{"userId":1,"userName":"Ruben Elizondo
updated again","subscribedOn":"2020-09-03 16:10:43"}]
which is fine, now from the original array i remove one element to get:
Array
(
[1] => stdClass Object
(
[userId] => 1
[userName] => Ruben Elizondo updated again
[subscribedOn] => 2020-09-03 16:10:43
)
)
and if i json_encode this i get:
{"1":{"userId":1,"userName":"Ruben Elizondo updated
again","subscribedOn":"2020-09-03 16:10:43"}}
so for arrays with 2 or more objects i get encoded a string that is a json representation of an
array of objects, but if i have only 1 element, the json encoded string is an object inside another
object
That's a big issue for my use case.