Check If array is null or not in php

后端 未结 6 1110
庸人自扰
庸人自扰 2020-12-16 12:15

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

The array is

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


        
6条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 12:58

    you can use

    empty($result) 
    

    to check if the main array is empty or not.

    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

    ex:

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

提交回复
热议问题