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

前端 未结 7 1779
情深已故
情深已故 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:21

    I always do (Object)[];, like:

    $json = (Object)[]; // [] could also be array()
    

    ... play around with it in PHP ...

    $json = json_encode($json);
    

    ... now it's real JSON ...

提交回复
热议问题