I have a PHP data structure I want to JSON encode. It can contain a number of empty arrays, some of which need to be encoded as arrays and some of which need to be encoded as objects.
For instance, lets say I have this data structure:
$foo = array(
"bar1" => array(), // Should be encoded as an object
"bar2" => array() // Should be encoded as an array
);
I would like to encode this into:
{
"bar1": {},
"bar2": []
}
But if I use json_encode($foo, JSON_FORCE_OBJECT)
I will get objects as:
{
"bar1": {},
"bar2": {}
}
And if I use json_encode($foo)
I will get arrays as:
{
"bar1": [],
"bar2": []
}
Is there any way to encode the data (or define the arrays) so I get mixed arrays and objects?
question from:
https://stackoverflow.com/questions/11670575/php-json-encode-json-force-object-mixed-object-and-array-output 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…