簡體   English   中英

檢查多維數組中的鍵的值是否為空

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

我需要一個簡單而優雅的解決方案來檢查鍵在多維數組中是否具有空值。 應該返回true / false。

像這樣,但是對於多維數組:

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

我發現的所有問題都是在多數組內搜索特定值,而不僅僅是檢查鍵是否為空並返回true / false。

這是一個例子:

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

這將返回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 
}

這也將返回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
}

這將返回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
}

當我說空的時候,我的意思是像這個empty()

更新:

我正在嘗試從這個問題改編代碼,但到目前為止沒有任何運氣。 這是我到目前為止所擁有的,修復任何幫助將不勝感激:

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

您必須調用遞歸函數,例如我有多維數組

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

將返回具有true和false的數組,具有鍵和索引的數組將包含true,具有索引但值不包含false的數組,您可以在檢查條件的地方使用此數組

下面的函數可以幫助您檢查嵌套數組中的空值。

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

用法:

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

注意:如果值是0,則此函數還返回false,因為使用了empty

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM