Check If array is null or not in php

后端 未结 6 1074
庸人自扰
庸人自扰 2020-12-16 12:15

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

The array is

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


        
相关标签:
6条回答
  • 2020-12-16 12:55

    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.

    0 讨论(0)
  • 2020-12-16 12:58

    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());
    
    0 讨论(0)
  • 2020-12-16 13:01

    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
    }
    
    0 讨论(0)
  • 2020-12-16 13:08

    This checks if the array is empty

    if (!empty($result) {
        // do stuf if array is not empty
    } else {
        // do stuf if array is empty
    }
    

    This checks if the array is null or not

    if (is_null($result) {
       // do stuf if array is null
    } else {
       // do stuf if array is not null
    }
    
    0 讨论(0)
  • 2020-12-16 13:08

    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;
      }
    }
    
    0 讨论(0)
  • 2020-12-16 13:19

    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;
    }
    
    0 讨论(0)
提交回复
热议问题