我有这样的数组
<?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;
}
}
}
答案 0 :(得分:1)
$empty = false;
foreach($array as $item)
{
if(empty($item[6])
{
$empty=true;
break;
}
}
return $empty;
答案 1 :(得分:0)
return
,因此定义一个布尔值,并在条件满足时将其设置为TRUE。break
来停止循环运行(保存运行时)
http://php.net/manual/en/control-structures.break.php ++ 1使用K&amp; R样式缩进,或不使用它。但是不要试试! ;) http://en.wikipedia.org/wiki/Indent_style#K.26R_style
$found = false;
foreach ($array as $item) {
foreach ($item as $key => $value) {
print($key);
if (6 == $key && NULL === $value) { // or use 'empty($value)'
echo "found";
$found = true;
break 2;
} else {
echo "not found";
}
}
}
return !$found;
答案 2 :(得分:0)
这是PHP&gt; = 5.5.0的替代品,用于检查''
,0
,null
和false
:
return !array_diff($six = array_column($array, 6), array_filter($six));