Reputation: 10485
SO to the rescue!
Say I have these arrays:
<?php
$arr_1 = array([0] => 'setup');
$arr_2 = array([0] => 'artwork', [1] => 'path');
$arr_3 = array([0] => 'artwork', [1] => 'color');
$container = array(
'progress' => array(
'setup' => 'complete',
'artwork' => array(
'path' => 'complete',
'color'=> '',
)
)
);
?>
What I want to do is check the $container
to see if the value
or values
from the given arrays are empty, basically giving the effect of:
if(empty($container['progress'][*first value of given array*][*if exists, second value of given array*])){...}
What is the best way to achieve said goal?
Upvotes: 1
Views: 271
Reputation: 1
You could use a function like this:
<?php
function isNestedArrayEmpty($parentArray, $parentKey, $childKeys)
{
if (empty($parentArray))
return TRUE;
$node = $parentArray[$parentKey];
if (empty($node))
return TRUE;
if (!empty($childKeys))
{
foreach ($childKeys as $key)
{
if (empty($node[$key]))
return TRUE;
$node = $node[$key];
}
}
return false;
}
?>
Then call the function like this:
if (isNestedArrayEmpty($container, 'progress', $arr_1)) { ... }
if (isNestedArrayEmpty($container, 'progress', $arr_2)) { ... }
if (isNestedArrayEmpty($container, 'progress', $arr_3)) { ... }
Here is a complete working example, using the arrays you provided. (Note: I removed the square brackets around the keys in the intializers for $arr_1
, $arr_2
and $arr_3
, as this seems to be a syntax error).
<html>
<body>
<?php
function isNestedArrayEmpty($parentArray, $parentKey, $childKeys)
{
if (empty($parentArray))
return TRUE;
$node = $parentArray[$parentKey];
if (empty($node))
return TRUE;
if (!empty($childKeys))
{
foreach ($childKeys as $key)
{
if (empty($node[$key]))
return TRUE;
$node = $node[$key];
}
}
return false;
}
$arr_1 = array(0 => 'setup');
$arr_2 = array(0 => 'artwork', 1 => 'path');
$arr_3 = array(0 => 'artwork', 1 => 'color');
$container = array(
'progress' => array(
'setup' => 'complete',
'artwork' => array(
'path' => 'complete',
'color'=> '',
)
)
);
echo '$container[\'progress\'] empty?: ';
if (isNestedArrayEmpty($container, 'progress', NULL)) {
echo 'Yes';
} else {
echo 'No';
}
echo '<br>';
echo '$container[\'progress\'][\'setup\'] empty?: ';
if (isNestedArrayEmpty($container, 'progress', $arr_1)) {
echo 'Yes';
} else {
echo 'No';
}
echo '<br>';
echo '$container[\'progress\'][\'artwork\'][\'path\'] empty?: ';
if (isNestedArrayEmpty($container, 'progress', $arr_2)) {
echo 'Yes';
} else {
echo 'No';
}
echo '<br>';
echo '$container[\'progress\'][\'artwork\'][\'color\'] empty?: ';
if (isNestedArrayEmpty($container, 'progress', $arr_3)) {
echo 'Yes';
} else {
echo 'No';
}
echo '<br>';
?>
</body>
</html>
Output from the above example:
$container['progress'] empty?: No
$container['progress']['setup'] empty?: No
$container['progress']['artwork']['path'] empty?: No
$container['progress']['artwork']['color'] empty?: Yes
Upvotes: 0
Reputation: 816600
Something like this:
function array_key_empty($array, $keys) {
foreach($keys as $key) {
if(!array_key_exists($key, $array)) {
return true;
}
else {
$array = $array[$key];
}
}
return empty($array);
}
I assume you also want to get true
if the keys don't exist.
Upvotes: 1