检查空数组的最佳方法是什么?

时间:2022-08-25 20:08:45

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

如何以递归方式检查数组中的空内容,如下例所示:

Array
(
    [product_data] => Array
        (
            [0] => Array
                (
                    [title] => 
                    [description] => 
                    [price] => 
                )

        )
    [product_data] => Array
        (
            [1] => Array
                (
                    [title] => 
                    [description] => 
                    [price] => 
                )

        )

)

The array is not empty but there is no content. How can I check this with a simple function?

该数组不是空的,但没有内容。如何通过简单的功能检查?

Thank!!

谢谢!!

10 个解决方案

#1


15  


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

#2


10  

If your array is only one level deep you can also do:

如果您的数组只有一个级别,您还可以执行以下操作:

if (strlen(implode('', $array)) == 0)

Works in most cases :)

适用于大多数情况:)

#3


7  

Solution with array_walk_recursive:

使用array_walk_recursive的解决方案:

function empty_recursive($value)
{
        if (is_array($value)) {
                $empty = TRUE;
                array_walk_recursive($value, function($item) use (&$empty) {
                        $empty = $empty && empty($item);
                });
        } else {
                $empty = empty($value);
        }
        return $empty;
}

#4


2  

Assuming the array will always contain the same type of data:

假设数组将始终包含相同类型的数据:

function TestNotEmpty($arr) {
    foreach($arr as $item)
        if(isset($item->title) || isset($item->descrtiption || isset($item->price))
            return true;
    return false;
}

#5


2  

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

#6


2  

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.

这是我的版本。一旦在数组中找到非空字符串,它就会停止。另外,它正确检查空字符串,因此0(零)不被视为空字符串(如果您使用empty()函数,则为空)。顺便说一句,即使使用这个函数只是为了字符串多年来证明是非常宝贵的。

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 ('')
}

#7


0  

If anyone stumbles on this question and needs to check if the entire array is NULL, meaning that each pair in the array is equal to null, this is a handy function. You could very easily modify it to return true if any variable returns NULL as well. I needed this for a certain web form where it updated users data and it was possible for it to come through completely blank, therefor not needing to do any SQL.

如果有人在这个问题上发现并且需要检查整个数组是否为NULL,这意味着数组中的每一对都等于null,这是一个方便的函数。如果任何变量也返回NULL,您可以非常轻松地修改它以返回true。我需要这个用于更新用户数据的某个Web表单,并且它可能完全空白,因此不需要执行任何SQL。

$test_ary = array("1"=>NULL, "2"=>NULL, "3"=>NULL);

function array_empty($ary, $full_null=false){
    $null_count = 0;
    $ary_count = count($ary);

    foreach($ary as $value){
        if($value == NULL){
            $null_count++;
        }
    }

    if($full_null == true){
        if($null_count == $ary_count){
            return true;
        }else{
            return false;
        }
    }else{
        if($null_count > 0){
            return true;
        }else{
            return false;
        }
    }
}

$test = array_empty($test_ary, $full_null=true);
echo $test;

#8


0  

$arr=array_unique(array_values($args));
if(empty($arr[0]) && count($arr)==1){
 echo "empty array";
}

#9


0  

Returns TRUE if passed a variable other than an array, or if any of the nested arrays contains a value (including falsy values!). Returns FALSE otherwise. Short circuits.

如果传递了一个非数组的变量,或者任何嵌套数组包含一个值(包括falsy值!),则返回TRUE。否则返回FALSE。短路。

function has_values($var) {
  if (is_array($var)) {
    if (empty($var)) return FALSE;
    foreach ($var as $val) {
      if(has_values($val)) return TRUE;
    }
    return FALSE;
  } 
  return TRUE;
}

#10


0  

Here's a good utility function that will return true (1) if the array is empty, or false (0) if not:

这是一个很好的实用函数,如果数组为空则返回true(1),否则返回false(0):

function is_array_empty( $mixed ) {
    if ( is_array($mixed) ) {
        foreach ($mixed as $value) {
            if ( ! is_array_empty($value) ) {
                return false;
            }
        }
    } elseif ( ! empty($mixed) ) {
        return false;
    }

    return true;
}

For example, given a multidimensional array:

例如,给定一个多维数组:

$products = array(
    'product_data' => array(
        0 => array(
            'title' => '',
            'description' => null,
            'price' => '',
        ),
    ),
);

You'll get a true value returned from is_array_empty(), since there are no values set:

您将从is_array_empty()返回一个真值,因为没有设置值:

var_dump( is_array_empty($products) );

View this code interactively at: http://codepad.org/l2C0Efab

以交互方式查看此代码:http://codepad.org/l2C0Efab

#1


15  


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

#2


10  

If your array is only one level deep you can also do:

如果您的数组只有一个级别,您还可以执行以下操作:

if (strlen(implode('', $array)) == 0)

Works in most cases :)

适用于大多数情况:)

#3


7  

Solution with array_walk_recursive:

使用array_walk_recursive的解决方案:

function empty_recursive($value)
{
        if (is_array($value)) {
                $empty = TRUE;
                array_walk_recursive($value, function($item) use (&$empty) {
                        $empty = $empty && empty($item);
                });
        } else {
                $empty = empty($value);
        }
        return $empty;
}

#4


2  

Assuming the array will always contain the same type of data:

假设数组将始终包含相同类型的数据:

function TestNotEmpty($arr) {
    foreach($arr as $item)
        if(isset($item->title) || isset($item->descrtiption || isset($item->price))
            return true;
    return false;
}

#5


2  

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

#6


2  

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.

这是我的版本。一旦在数组中找到非空字符串,它就会停止。另外,它正确检查空字符串,因此0(零)不被视为空字符串(如果您使用empty()函数,则为空)。顺便说一句,即使使用这个函数只是为了字符串多年来证明是非常宝贵的。

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 ('')
}

#7


0  

If anyone stumbles on this question and needs to check if the entire array is NULL, meaning that each pair in the array is equal to null, this is a handy function. You could very easily modify it to return true if any variable returns NULL as well. I needed this for a certain web form where it updated users data and it was possible for it to come through completely blank, therefor not needing to do any SQL.

如果有人在这个问题上发现并且需要检查整个数组是否为NULL,这意味着数组中的每一对都等于null,这是一个方便的函数。如果任何变量也返回NULL,您可以非常轻松地修改它以返回true。我需要这个用于更新用户数据的某个Web表单,并且它可能完全空白,因此不需要执行任何SQL。

$test_ary = array("1"=>NULL, "2"=>NULL, "3"=>NULL);

function array_empty($ary, $full_null=false){
    $null_count = 0;
    $ary_count = count($ary);

    foreach($ary as $value){
        if($value == NULL){
            $null_count++;
        }
    }

    if($full_null == true){
        if($null_count == $ary_count){
            return true;
        }else{
            return false;
        }
    }else{
        if($null_count > 0){
            return true;
        }else{
            return false;
        }
    }
}

$test = array_empty($test_ary, $full_null=true);
echo $test;

#8


0  

$arr=array_unique(array_values($args));
if(empty($arr[0]) && count($arr)==1){
 echo "empty array";
}

#9


0  

Returns TRUE if passed a variable other than an array, or if any of the nested arrays contains a value (including falsy values!). Returns FALSE otherwise. Short circuits.

如果传递了一个非数组的变量,或者任何嵌套数组包含一个值(包括falsy值!),则返回TRUE。否则返回FALSE。短路。

function has_values($var) {
  if (is_array($var)) {
    if (empty($var)) return FALSE;
    foreach ($var as $val) {
      if(has_values($val)) return TRUE;
    }
    return FALSE;
  } 
  return TRUE;
}

#10


0  

Here's a good utility function that will return true (1) if the array is empty, or false (0) if not:

这是一个很好的实用函数,如果数组为空则返回true(1),否则返回false(0):

function is_array_empty( $mixed ) {
    if ( is_array($mixed) ) {
        foreach ($mixed as $value) {
            if ( ! is_array_empty($value) ) {
                return false;
            }
        }
    } elseif ( ! empty($mixed) ) {
        return false;
    }

    return true;
}

For example, given a multidimensional array:

例如,给定一个多维数组:

$products = array(
    'product_data' => array(
        0 => array(
            'title' => '',
            'description' => null,
            'price' => '',
        ),
    ),
);

You'll get a true value returned from is_array_empty(), since there are no values set:

您将从is_array_empty()返回一个真值,因为没有设置值:

var_dump( is_array_empty($products) );

View this code interactively at: http://codepad.org/l2C0Efab

以交互方式查看此代码:http://codepad.org/l2C0Efab