best way to check a empty array?

后端 未结 11 1096
难免孤独
难免孤独 2020-11-30 10:16

How can I check an array recursively for empty content like this example:

Array
(
    [product_data] => Array
        (
            [0] => Array
               


        
相关标签:
11条回答
  • 2020-11-30 10:54

    Here's my version. Once it finds a non-empty string in an array, it stops. Plus it properly checks on empty strings, so that a 0 (zero) is not considered an empty string (which would be if you used empty() function). By the way even using this function just for strings has proven invaluable over the years.

    function isEmpty($stringOrArray) {
        if(is_array($stringOrArray)) {
            foreach($stringOrArray as $value) {
                if(!isEmpty($value)) {
                    return false;
                }
            }
            return true;
        }
    
        return !strlen($stringOrArray);  // this properly checks on empty string ('')
    }
    
    0 讨论(0)
  • 2020-11-30 10:54
    $arr=array_unique(array_values($args));
    if(empty($arr[0]) && count($arr)==1){
     echo "empty array";
    }
    
    0 讨论(0)
  • 2020-11-30 10:57

    Short circuiting included.

    function hasValues($input, $deepCheck = true) {
        foreach($input as $value) {
            if(is_array($value) && $deepCheck) {
                if($this->hasValues($value, $deepCheck))
                    return true;
            }
            elseif(!empty($value) && !is_array($value))
                return true;
        }
        return false;
    }
    
    0 讨论(0)
  • 2020-11-30 11:06

    I needed a function to filter an array recursively for non empty values.

    Here is my recursive function:

    function filterArray(array $array, bool $keepNonArrayValues = false): array {
      $result = [];
      foreach ($array as $key => $value) {
        if (is_array($value)) {
          $value = $this->filterArray($value, $keepNonArrayValues);
        }
    
        // keep non empty values anyway
        // otherwise only if it is not an array and flag $keepNonArrayValues is TRUE 
        if (!empty($value) || (!is_array($value) && $keepNonArrayValues)) {
          $result[$key] = $value;
        }
      }
    
      return array_slice($result, 0)
    }
    

    With parameter $keepNonArrayValues you can decide if values such 0 (number zero), '' (empty string) or false (bool FALSE) shout be kept in the array. In other words: if $keepNonArrayValues = true only empty arrays will be removed from target array.

    array_slice($result, 0) has the effect that numeric indices will be rearranged (0..length-1).

    Additionally, after filtering the array by this function it can be tested with empty($filterredArray).

    0 讨论(0)
  • 2020-11-30 11:07
    
    function is_array_empty($InputVariable)
    {
       $Result = true;
    
       if (is_array($InputVariable) && count($InputVariable) > 0)
       {
          foreach ($InputVariable as $Value)
          {
             $Result = $Result && is_array_empty($Value);
          }
       }
       else
       {
          $Result = empty($InputVariable);
       }
    
       return $Result;
    }
    
    0 讨论(0)
提交回复
热议问题