26

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?

5
  • Did you tried if(empty($result) || empty($result['Tags']))??
    – SERPRO
    Nov 9, 2011 at 16:36
  • if(empty($result['Tags'][0])) maybe?
    – janoliver
    Nov 9, 2011 at 16:36
  • Have you tried isset is_array is_null . . . ?
    – Mob
    Nov 9, 2011 at 16:37
  • 2
    An array cannot be null. If it's null, then it is not an array: it is null. Nov 9, 2011 at 17:31
  • 1
    Also, it's time for you to accept some previous answers. There are answers that you've suggested in comments are correct and useful in at least several of your questions. Nov 9, 2011 at 17:32

6 Answers 6

55

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());
8

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
}
3

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;
  }
}
1
  • 2
    the break has no effect since return true will quit the process. Your code is missing a return statement in cas is_array returns false. Feb 23, 2017 at 20:17
3

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;
}
1

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.

1

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
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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