How to check if JSON object is empty in PHP?

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

    Try without using empty() which is:

    get_object_vars($obj) ? TRUE : FALSE;
    

    On PHP docs we can read the note:

    When using empty() on inaccessible object properties, the __isset() overloading method will be called, if declared.

    Which means when using empty() on an object which is having __get() method, it will always return True.

    0 讨论(0)
  • 2020-12-15 05:38

    I had to tell if an object was empty or not, but I also had to ignore private and protected properties, so I made this little function with which you can do this.

    function empty_obj(&$object, $ignore_private = true, $ignore_protected = true) {
      $obj_name = get_class($object);
      $obj = (array)$object;
    
      foreach(array_keys($obj) as $prop) {
        $is_private = $is_protected = false;
    
        $prop = preg_replace("/[^\w*]/", '', $prop);
        $prop_name = str_replace(array($obj_name, '*'), '', $prop);
    
        if(preg_match("~^$obj_name$prop_name$~", $prop))
          $is_private = true;
        if(preg_match("~^\*$prop_name$~", $prop))
          $is_protected = true;
    
        if(!$is_private || !$is_protected || ($is_private && !$ignore_private) || ($is_protected && !$ignore_protected))
          return;
      }
      return true;
    }
    
    0 讨论(0)
  • 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;
    }
    
    0 讨论(0)
  • 2020-12-15 05:45

    How many objects are you unserializing? Unless empty(get_object_vars($object)) or casting to array proves to be a major slowdown/bottleneck, I wouldn't worry about it – Greg's solution is just fine.

    I'd suggest using the the $associative flag when decoding the JSON data, though:

    json_decode($data, true)
    

    This decodes JSON objects as plain old PHP arrays instead of as stdClass objects. Then you can check for empty objects using empty() and create objects of a user-defined class instead of using stdClass, which is probably a good idea in the long run.

    0 讨论(0)
  • 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))
        ...
    
    0 讨论(0)
提交回复
热议问题