0

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;
}
TinyTiger
  • 1,801
  • 7
  • 47
  • 92
  • do you want to check in multi dimension array key have value?? – nageen nayak May 16 '18 at 04:11
  • @nageennayak yes. Return true if the key has a value, return false if no value. – TinyTiger May 16 '18 at 04:13
  • You have to proper search and get your answer https://stackoverflow.com/questions/6990855/php-check-if-value-and-key-exist-in-multidimensional-array – nageen nayak May 16 '18 at 04:16
  • @nageennayak I saw that, but it's searching for a specific value. Not returning true or false if empty. – TinyTiger May 16 '18 at 04:17
  • @nageennayak this looked most interesting `$key = array_search(40489, array_column($userdb, 'uid'));` but again, its searching for a value. Not returning true/false if empty. – TinyTiger May 16 '18 at 04:19
  • You have to call recursive function and make array who have contain true and false respective key in the multi dimensional array and use array as condition in your code. as i mentioned in my answer you can check your array and adjust into your code.i hope it will help you. – nageen nayak May 16 '18 at 05:47
  • @nageennayak Thanks for the help, but I can't see how your answer would work since the function only takes one arg. I have updated my question with some code adapted from the other question you posted, but I can't get it working. Any chance you could take a look? – TinyTiger May 16 '18 at 08:20

2 Answers2

0

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

nageen nayak
  • 1,262
  • 2
  • 18
  • 28
  • I don't think this will work. That function only takes one parameter, $array. I need two parameters, $array to define the array to check, and $key to define the key I want to check is not empty. – TinyTiger May 16 '18 at 04:43
  • Also this does not return true or false. It needs to check if the key is empty, and return true or false. – TinyTiger May 16 '18 at 04:44
  • Please post your test array and key, also post what you output want. so i get idea exactly what you want. – nageen nayak May 16 '18 at 04:47
  • Updated again, with better examples. – TinyTiger May 16 '18 at 05:31
0

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

Jaydeep Mor
  • 1,690
  • 3
  • 21
  • 39