How to check if JSON object is empty in PHP?

后端 未结 5 2133
别那么骄傲
别那么骄傲 2020-12-15 04:57

I\'m reading JSON data with PHP and that data contains empty objects (like {}). So the problem is, I have to handle the case when object is empty in different m

5条回答
  •  长情又很酷
    2020-12-15 05:48

    You could cast it to an array (unfortunately you can't do this within a call to empty():

    $x = (array)$obj;
    if (empty($x))
        ...
    

    Or cast to an array and count():

    if (count((array)$obj))
        ...
    

提交回复
热议问题