|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2011-04-12 13:26 UTC] samu dot voutilainen at gmail dot com
Description:
------------
If you have empty array inside object, json_encode() does encode the value into JSON format, but when the data is decoded with json_decode(), this empty JSON array is omitted from output.
Consider following structure:
["timetable"]=>
object(pizzaonline\model\Timetable)#30 (9) {
["id"]=>
string(3) "774"
["timetable"]=>
array(0) {
}
["opening_hours"]=>
..
Corresponding json_encode would be:
"timetable":{
"id":"774",
"timetable":[
],
"opening_hours":[
..
This is all right, but I have a loop that removes this empty array with unset(). So PHP structure from JSON is encoded is like:
["timetable"]=>
object(pizzaonline\model\Timetable)#30 (8) {
["id"]=>
string(3) "774"
["opening_hours"]=>
But using json_encode() returns same result that is in snippet 2.
Just ask if any questions, I’ll do example script later...
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sun Nov 02 02:00:01 2025 UTC |
I need a reproduce script. I just used: <?php $test = new StdClass; $test->a = 1; $test->b = array(); $test->c = 'foo'; $json = json_encode($test); var_dump($test); var_dump(json_decode($json)); And I got the output: object(stdClass)#1 (3) { ["a"]=> int(1) ["b"]=> array(0) { } ["c"]=> string(3) "foo" } object(stdClass)#2 (3) { ["a"]=> int(1) ["b"]=> array(0) { } ["c"]=> string(3) "foo" } Nothing missing there.