2011-07-12 62 views
1

所以救援!检查阵列与另一个阵列的内容

说我有这些阵列:

<?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*])){...} 

达到上述目标的最佳方法是什么?

回答

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如果不存在的钥匙。

+0

优雅的解决方案,谢谢Felix! –

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