Reputation: 1166
i have array like this
<?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 => ,
),
);
and i want to search in this multi-array to find if the key [6] => is empty.if it was empty in any array return false so how to do this
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;
}
}
}
Upvotes: 0
Views: 833
Reputation: 17289
$empty = false;
foreach($array as $item)
{
if(empty($item[6]))
{
$empty=true;
break;
}
}
return $empty;
Upvotes: 1
Reputation: 78994
Here's an alternate for PHP >= 5.5.0 that checks for ''
, 0
, null
and false
:
return !array_diff($six = array_column($array, 6), array_filter($six));
Upvotes: 0
Reputation: 99
return
in a function, so define a boolean value, and turn it to TRUE when the condition satisfies.break
to stop the cycle(s) running (saves runtime)
http://php.net/manual/en/control-structures.break.php++1 Use K&R style indent, or don't use it. But do not try! ;) 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;
Upvotes: 0