Skip to content
Advertisement

Check If array is null or not in php

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

The array is

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

    )
  )

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?

Advertisement

Answer

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());
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement