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?
To check if an array is null, use equal to operator and check if array is equal to the value null. In the following example, we will initialize an integer array with null. And then use equal to comparison operator in an If Else statement to check if array is null. The array is empty.
An empty array is falsey in PHP, so you don't even need to use empty() as others have suggested. PHP's empty() determines if a variable doesn't exist or has a falsey value (like array() , 0 , null , false , etc).
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With