这是一个我正在努力解决的有趣问题。我有一个多维数组:
这应该返回true
array(
array('id' = > 23, 'address' => '123 Grove Street, Toronto'),
array('id' = > 24, 'address' => ''),
array('id' = > 25, 'address' => ''),
array('id' = > 26, 'address' => '123 Grove Street, Toronto')
)
这应该返回false
array(
array('id' = > 23, 'address' => ''),
array('id' = > 24, 'address' => ''),
array('id' = > 25, 'address' => ''),
array('id' = > 26, 'address' => '')
)
我想测试上面的数组,看看是否有任何结果在地址字段中有值。所以第一个例子会返回TRUE,但是下面会返回false。用法是仅在有地址结果时才会出现的元素。
答案 0 :(得分:4)
function hasAtLeastOneAddress($arr)
{
foreach($arr as $subarr)
{
if($subarr['address'] != '')
{
return true;
}
}
return false;
}