检查如果数组为空或不在PHP [英] Check If array is null or not in php

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

问题描述

我有一个数组像下面这是通过解析XML URL生成的。

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

该数组是

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

    )
  )

数组名是 $结果。现在,我要检查,如果收到类似上面我想打印失败的消息的数组。但如何在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对象,你需要查询对象,如果它是空的或没有。见<一href=\"http://www.php.net/manual/en/simplexmlelement.count.php\">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天全站免登陆