Hướng dẫn check empty json php

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 manner but I can't find good enough way to do the check. empty(get_object_vars(object)) looks too scary and very inefficient. Is there good way to do the check?

YakovL

6,91012 gold badges57 silver badges88 bronze badges

asked Sep 7, 2009 at 13:28

1

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.

answered Sep 7, 2009 at 14:07

1

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))
    ...

answered Sep 7, 2009 at 13:37

GregGreg

310k53 gold badges366 silver badges328 bronze badges

4

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.

answered Mar 4, 2015 at 22:18

kenorbkenorb

144k76 gold badges655 silver badges710 bronze badges

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;
}

answered Jul 27, 2011 at 13:06

ParziphalParziphal

5,5523 gold badges35 silver badges34 bronze badges

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;
}

answered Sep 11, 2014 at 15:03

AdamAdam

6,5243 gold badges40 silver badges60 bronze badges