测试数组中的任何结果是否在特定节点中具有值
问题描述:
Here is a fun issue I am wrestling with. I have a Multi Dimensional array:
This should return 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')
)
This should return false
array(
array('id' = > 23, 'address' => ''),
array('id' = > 24, 'address' => ''),
array('id' = > 25, 'address' => ''),
array('id' = > 26, 'address' => '')
)
I want to test the above array to see if ANY of the the results have a value in the address field. So the first example would return TRUE but below would return false. The usage is for an element that will only appear when there are results with addresses.
这是一个我正在努力解决的有趣问题。 我有一个多维数组: p>
这应该返回true p>
数组(
array('id'=> 23, 'address'=>'123 Grove Street,Toronto'),
array('id'=> 24,'address'=>''),
array('id'=> 25,' 地址'=>''),
array('id'=> 26,'地址'=>'123 Grove Street,多伦多')
)
code> pre>
这应该返回false p>
数组(
array('id'=> 23,'address'=>''),
数组 ('id'=> 24,'地址'=>''),
数组('id'=> 25,'地址'=>''),
数组('id'=> ; 26,'address'=>'')
)
code> pre>
我想测试上面的数组以查看结果中是否有任何值 在地址字段中。 所以第一个例子会返回TRUE,但是下面会返回false。 用法是仅在有地址结果时才会出现的元素。 p>
div>
答
function hasAtLeastOneAddress($arr)
{
foreach($arr as $subarr)
{
if($subarr['address'] != '')
{
return true;
}
}
return false;
}