Best way to create an empty object in JSON with PHP?

前端 未结 7 1788
情深已故
情深已故 2020-11-27 03:26

To create an empty JSON object I do usually use:

json_encode((object) null);

casting null to an object works, but is there any other prefer

7条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 04:29

    To create an empty object in JSON with PHP I used

    $json=json_decode('{}');
    $json->status=202;
    $json->message='Accepted';
    print_r($json);
    

    which produced

    stdClass Object
    (
        [status] => 202
        [message] => Accepted
    )
    

    which is necessary, because later I have to do this

    if(is_object($json))
    

提交回复
热议问题