How check 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?

Mosh Feu

27.1k15 gold badges85 silver badges126 bronze badges

asked Nov 9, 2011 at 16:34

How check array is null or not in php?

Manish JangirManish Jangir

5,1994 gold badges35 silver badges70 bronze badges

5

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)) {
    return false;
}
if ( !($result['Tags'] instanceof SimpleXMLElement)) {
    return false;
}
return ($result['Tags']->count());

How check array is null or not in php?

answered Nov 9, 2011 at 16:50

This checks if the variable is not set or contains a falsey value (zero, empty string, empty array, etc)

if (!empty($result) {
    // do stuff if the variable is not empty
} else {
    // do stuff if the variable is empty
}

This checks if the the variable is null

if (is_null($result) {
   // do stuff if the variable is null
} else {
   // do stuff if the variable is not null
}

How check array is null or not in php?

answered Dec 23, 2016 at 6:17

Anish RaiAnish Rai

6909 silver badges16 bronze badges

Corrected;

/*
 return true if the array is not empty
 return false if it is empty
*/
function is_array_empty($arr){
  if(is_array($arr)){     
      foreach($arr as $key => $value){
          if(!empty($value) || $value != NULL || $value != ""){
              return true;
              break;//stop the process we have seen that at least 1 of the array has value so its not empty
          }
      }
      return false;
  }
}

answered Sep 13, 2013 at 21:00

WebbuWebbu

665 bronze badges

1

Right code of two ppl before ^_^

/* return true if values of array are empty
*/
function is_array_empty($arr){
   if(is_array($arr)){
      foreach($arr as $value){
         if(!empty($value)){
            return false;
         }
      }
   }
   return true;
}

answered Nov 10, 2017 at 16:14

How check array is null or not in php?

I understand what you want. You want to check every data of the array if all of it is empty or at least 1 is not empty

Empty array

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

Not an Empty array

Array ( [Tags] => SimpleXMLElement Object ( [0] =>,[1] => "s" ) )


I hope I am right. You can use this function to check every data of an array if at least 1 of them has a value.

/*
 return true if the array is not empty
 return false if it is empty
*/
function is_array_empty($arr){
  if(is_array($arr)){     
      foreach($arr $key => $value){
          if(!empty($value) || $value != NULL || $value != ""){
              return true;
              break;//stop the process we have seen that at least 1 of the array has value so its not empty
          }
      }
      return false;
  }
}

if(is_array_empty($result['Tags'])){
    //array is not empty
}else{
    //array is empty
}

Hope that helps.

answered Nov 9, 2011 at 21:51

if array is look like this [null] or [null, null] or [null, null, null, ...]

you can use implode:

implode is use for convert array to string.

if(implode(null,$arr)==null){
     //$arr is empty
}else{
     //$arr has some value rather than null
}

answered Jan 13, 2018 at 9:27

Not the answer you're looking for? Browse other questions tagged php arrays null or ask your own question.

How do you check if an array is null?

To check if all of the values in an array are equal to null , use the every() method to iterate over the array and compare each value to null , e.g. arr. every(value => value === null) . The every method will return true if all values in the array are equal to null .

IS NULL check in PHP?

The is_null() function checks whether a variable is NULL or not. This function returns true (1) if the variable is NULL, otherwise it returns false/nothing.

Is null or empty PHP?

is_null() The empty() function returns true if the value of a variable evaluates to false . This could mean the empty string, NULL , the integer 0 , or an array with no elements. On the other hand, is_null() will return true only if the variable has the value NULL .

How do you check if a value is in an array PHP?

The in_array() function is an inbuilt function in PHP that is used to check whether a given value exists in an array or not. It returns TRUE if the given value is found in the given array, and FALSE otherwise.