简体   繁体   English

检查多维数组中的键的值是否为空

[英]Check if a value is empty for a key in a multidimensional array

I need a simple and elegant solution to check if a key has an empty value in a multidimensional array. 我需要一个简单而优雅的解决方案来检查键在多维数组中是否具有空值。 Should return true/false. 应该返回true / false。

Like this, but for a multidimensional array: 像这样,但是对于多维数组:

if (empty($multi_array["key_inside_multi_array"])) {
  // Do something if the key was empty
}

All the questions I have found are searching for a specific value inside the multi-array, not simply checking if the key is empty and returning a true/false. 我发现的所有问题都是在多数组内搜索特定值,而不仅仅是检查键是否为空并返回true / false。

Here is an example: 这是一个例子:

$my_multi_array = array(    
        0 =>  array(  
            "country"   => "",  
            "price"    =>  4,  
            "discount-price"    =>  0,  
        ),  
);

This will return true: 这将返回true:

$my_key = "country";

if (check_multi_array_for_empty_value($my_multi_array, $my_key)) {
  //Run this code here because the "country" key in my multi dimensional array is empty 
}

This will also return true: 这也将返回true:

$my_key = "discount-price";

if (check_multi_array_for_empty_value($my_multi_array, $my_key)) {
  //Run this code here because the "discount-price" key in my multi dimensional array is empty
}

This will return false: 这将返回false:

$my_key = "price";

if (check_multi_array_for_empty_value($my_multi_array, $my_key)) {
  //This WILL NOT RUN because the "price" key in my multi dimensional array is NOT empty
}

When I say empty, I mean like this empty() 当我说空的时候,我的意思是像这个empty()

UPDATE: 更新:

I am trying to adapt the code from this question but so far without any luck. 我正在尝试从这个问题改编代码,但到目前为止没有任何运气。 Here is what I have so far, any help fixing would be appreciated: 这是我到目前为止所拥有的,修复任何帮助将不胜感激:

function bg_empty_value_check($array, $key)
{
    foreach ($array as $item)
    {
        if (is_array($item) && bg_empty_value_check($item, $key)) return true;
        if (empty($item[$key])) return true;
    }
    return false;
}

You have to call recursive function for example i have multi dimension array 您必须调用递归函数,例如我有多维数组

function checkMultiArrayValue($array) {
        global $test;
        foreach ($array as $key => $item) {

            if(!empty($item) && is_array($item)) {
                checkMultiArrayValue($item);
            }else {
                if($item)
                 $test[$item] = false;
                else
                 $test[$item] = true;
            }
        }
        return $test;   
    }

 $multiArray = array(    
                0 =>  array(  
                      "country"   => "",  
                      "price"    => 4,  
                      "discount-price" => 0,  
               ),);

$test = checkMultiArrayValue($multiArray);
echo "<pre>"
print_r($test);

Will return array with true and false, who have key and index will contain true and who have index but not value contain false, you can use this array where you check your condition 将返回具有true和false的数组,具有键和索引的数组将包含true,具有索引但值不包含false的数组,您可以在检查条件的地方使用此数组

Below function may help you to check empty value in nested array. 下面的函数可以帮助您检查嵌套数组中的空值。

function boolCheckEmpty($array = [], $key = null)
{
    if (array_key_exists($key, $array) && !empty($array[$key])) {
        return true;
    }
    if (is_array($array)) {
        foreach ((array)$array as $arrKey => $arrValue) {
            if (is_array($arrValue)) {
                return boolCheckEmpty($arrValue, $key);
            }
            if ($arrKey == $key && !empty($arrValue)) {
                return $arrValue;
            }
        }
    }
    return false;
}

Usage : 用法:

$my_multi_array = array(    
    0 =>  array(  
        "country"   => "aa",  
        "price"    =>  1,  
        "discount-price"    =>  0,  
    ),  
);
// Call
$checkEmpty = $this->boolCheckEmpty($my_multi_array, 'price');
var_dump($checkEmpty);

Note : This function also return false if value is 0 because used of empty 注意:如果值是0,则此函数还返回false,因为使用了empty

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM