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