在php中检查数组是否为空 [英] Check If array is null or not in php

查看:43
本文介绍了在php中检查数组是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的数组,它是通过解析 xml url 生成的.

I have an array like below which is generated by parsing a xml url.

数组是

Array
  (
 [Tags] => SimpleXMLElement Object
    (
        [0] => 

    )
  )

数组名是$result.现在我想检查数组是否像上面那样接收到我想打印一条失败消息.但是如何在 if 条件下检查这个数组?

The array name is $result. Now I want to check that if the array received like above I want to print a message of failure. But how to check this array in if condition?

推荐答案

你可以使用

empty($result) 

检查主数组是否为空.

但是由于您有一个 SimpleXMLElement 对象,您需要查询该对象是否为空.请参阅 http://www.php.net/manual/en/simplexmlelement.count.php

But since you have a SimpleXMLElement object, you need to query the object if it is empty or not. See http://www.php.net/manual/en/simplexmlelement.count.php

例如:

if (empty($result) || !isset($result['Tags'])) {
    return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
    return false;
}
return ($result['Tags']->count());

这篇关于在php中检查数组是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆