How to check if JSON object is empty in PHP?

后端 未结 5 2129
别那么骄傲
别那么骄傲 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:43

    I am not sure if this is more or less effective than casting to an array but I would guess more. You could just start to loop the object and as soon as you find something you have an answer and stop looping.

    function is_obj_empty($obj){
       if( is_null($obj) ){
          return true;
       }
       foreach( $obj as $key => $val ){
          return false;
       }
       return true;
    }
    

提交回复
热议问题