Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check If array is null or not in php

Tags:

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?

like image 725
Manish Jangir Avatar asked Nov 09 '11 16:11

Manish Jangir


People also ask

How do you check if an array value is not null?

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.

Is array empty in PHP?

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).


1 Answers

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()); 
like image 179
Martin Samson Avatar answered Sep 28 '22 04:09

Martin Samson