I have a loop like this:
foreach ($result_new as $count){
echo "<pre>"; print_r($count);
}
This is what is produced:
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
)
)
)
)
What I need it to find out how many times "ADDRESS CHECK FAILED" appears in each iteration and then do a comparison. I have tried this:
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";
}
The problem is that the $address value continues to increment with each loop but I just want that loops current total.
Just need to reset $address value at the end of the foreach loop, so every time it will count the current element values instead of the entire loop
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;
}
If I'm understanding the problem correctly, I think you need to do something like this:
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;
}
Then, you'd use it like so:
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";
}
}