代码改变世界

PHP json_encode() 序列化对象、数组、空对象、空数组

2020-03-24 20:51  小伍2013  阅读(408)  评论(0编辑  收藏  举报
$result = [
    "object" => ["a" => 1],
    "array" => [1,2,3,4,5],
    "empty_object0" => new stdClass(),
    "empty_object1" => new ArrayObject(),
    "empty_object2" => (object)[],
    "empty_array" => [],
    "array_object" => [
        ["a" => 1],
        ["b" => 2]
    ]
];
echo json_encode($result);

结果:

{
    "object":{"a":1},
    "array":[1,2,3,4,5],
    "empty_object0":{},
    "empty_object1":{},
    "empty_object2":{},
    "empty_array":[],
    "array_object":[{"a":1},{"b":2}]
}