Check If array is null or not in php

后端 未结 6 1073
庸人自扰
庸人自扰 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.

提交回复
热议问题