检查数组的内容与另一个数组

时间:2011-07-12 20:37:45

标签: php 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'=> '',
         )
    )
);

?>

我想要做的是检查$container以查看给定数组中的valuevalues是否为空,基本上会产生以下效果:

if(empty($container['progress'][*first value of given array*][*if exists, second value of given array*])){...}

实现上述目标的最佳方式是什么?

2 个答案:

答案 0 :(得分:1)

这样的事情:

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

我假设如果密钥不存在,您还希望获得true

答案 1 :(得分:0)

你可以使用这样的函数:

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

然后调用这个函数:

if (isNestedArrayEmpty($container, 'progress', $arr_1)) { ... }
if (isNestedArrayEmpty($container, 'progress', $arr_2)) { ... }
if (isNestedArrayEmpty($container, 'progress', $arr_3)) { ... }

这是一个完整的工作示例,使用您提供的阵列。 (注意:我删除了$arr_1$arr_2$arr_3的初个化程序中键旁边的方括号,因为这似乎是语法错误。)

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

以上示例的输出:

$container['progress'] empty?: No
$container['progress']['setup'] empty?: No
$container['progress']['artwork']['path'] empty?: No
$container['progress']['artwork']['color'] empty?: Yes