如何在多维数组中找到特定键的空值

时间:2015-02-24 15:13:58

标签: php multidimensional-array

我有这样的数组

<?php
$array =
    array
    (
        array (
            0 => 1,
            1 => 'php',
            2 => 11,
            3 => 11,
            4 => 11,
            5 => 11,
            6 => 11,
        ),
        array (
            0 => 1,
            1 => 'php',
            2 => 11,
            3 => 11,
            4 => 11,
            5 => 11,
            6 => ,
        ),

    );

我希望在这个多数组中搜索以查找密钥[6] =&gt;是空的。如果它在任何数组中为空,则返回false,以便如何执行此操作

foreach($array as $item)
{
    foreach($item as $key=>$value)
    {
        print($key);
        if($key=="6" && $value==NULL)
        {
            echo "found";
            return false;
        }else{
            echo "not found";
            return true;
        }
    }
}

3 个答案:

答案 0 :(得分:1)

$empty = false;
foreach($array as $item)
{
    if(empty($item[6])
    {
            $empty=true;
            break;
    }
}
return $empty;

答案 1 :(得分:0)

答案 2 :(得分:0)

这是PHP&gt; = 5.5.0的替代品,用于检查''0nullfalse

return !array_diff($six = array_column($array, 6), array_filter($six));