欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-05-18 23:48:34
结果: ......
$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}]
}