如何检查 JSON 对象在 PHP 中是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1389431/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:21:35  来源:igfitidea点击:

How to check if JSON object is empty in PHP?

phpjson

提问by vava

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?

我正在使用 PHP 读取 JSON 数据,该数据包含空对象(如{})。所以问题是,我必须以不同的方式处理对象为空的情况,但我找不到足够好的方法来进行检查。empty(get_object_vars(object))看起来太可怕了,而且效率很低。有什么好办法进行检查吗?

回答by ?ystein Riiser Gundersen

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.

你反序列化了多少个对象?除非empty(get_object_vars($object))或强制转换到 array 被证明是一个主要的减速/瓶颈,否则我不会担心 - Greg 的解决方案很好。

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

不过,我建议$associative在解码 JSON 数据时使用该标志:

json_decode($data, true)

This decodes JSON objects as plain old PHP arrays instead of as stdClassobjects. 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.

这将 JSON 对象解码为普通的旧 PHP 数组而不是stdClass对象。然后您可以使用empty()和创建用户定义类的对象来检查空对象,而不是使用stdClass,从长远来看,这可能是一个好主意。

回答by Greg

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

您可以将其转换为数组(不幸的是,您不能在调用中执行此操作empty()

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

Or cast to an array and count():

或转换为数组和count()

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

回答by kenorb

Try without using empty()which is:

尝试不使用empty()which 是:

get_object_vars($obj) ? TRUE : FALSE;

On PHP docswe can read the note:

PHP 文档上,我们可以阅读注释:

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

empty()在不可访问的对象属性上使用时,__isset()重载方法将被调用(如果已声明)。

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

这意味着在empty()具有__get()方法的对象上使用时,它将始终返回 True。

回答by Parziphal

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

回答by Adam

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