简体   繁体   中英

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.

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.

Here is an example:

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

This will return 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:

$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:

$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()

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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