我有一个这样的循环:
foreach ($result_new as $count){
echo "<pre>"; print_r($count);
}
这是产生的:
Array
(
[0] => Array
(
[0] => TomVanhecker
[1] => PASS
[2] => Array
(
[0] => Array
(
)
)
)
)
Array
(
[0] => Array
(
[0] => DonLay
[1] => PASS
[2] => Array
(
[0] => Array
(
[0] => ADDRESS CHECK FAILED
)
)
)
)
Array
(
[0] => Array
(
[0] => PetraBerumen
[1] => REVIEW
[2] => Array
(
[0] => Array
(
[0] => ADDRESS CHECK FAILED
)
)
)
[1] => Array
(
[0] => PetraBerumen
[1] => REVIEW
[2] => Array
(
[0] => Array
(
[0] => ADDRESS CHECK FAILED
)
)
)
[2] => Array
(
[0] => PetraBerumen
[1] => REVIEW
[2] => Array
(
[0] => Array
(
[0] => ADDRESS CHECK FAILED
)
)
)
)
我需要它来找出每次迭代中出现“ADDRESS CHECK FAILED”多少次然后进行比较。我试过这个:
foreach ($result_new as $count){
if ((in_array_r("ADDRESS CHECK FAILED", $count)) ){
$address++
}
if($address > 2){
echo "There was more than two address failures for this customer";
}
问题是$ address值随着每个循环继续递增但我只想要循环当前总数。
答案 0 :(得分:1)
只需要在foreach循环结束时重置$ address值,这样每次它都会计算当前元素值而不是整个循环
foreach ($result_new as $count){
if ((in_array_r("ADDRESS CHECK FAILED", $count)) ){
$address++;
}
if($address > 2){
echo "There was more than two address failures for this customer";
}
$address = 0;
}
答案 1 :(得分:0)
如果我正确理解问题,我认为你需要做这样的事情:
function in_array_recursive_count($needle, $haystack, &$current = 0, $strict = false) {
foreach ($haystack as $item) {
if ($strict ? $item === $needle : $item == $needle) {
$current++;
}
else if(is_array($haystack)) {
in_array_recursive_count($needle, $haystack, $current, $strict);
}
}
return $current;
}
然后,你会这样使用它:
foreach ($result_new as $count){
$address = in_array_recursive_count("ADDRESS CHECK FAILED", $count);
if($address > 2){
echo "There was more than two address failures for this customer";
}
}