0

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.

2
  • 1
    "The problem is that the $address value continues to increment with each loop but I just want that loops current total." I'm assuming you mean you want to count the total number of occurrences in each $count array correct? If that's the case, the in_array_r function as defined here: gist.github.com/Billy-/bc6865066981e80e097f isn't going to cut it since it returns a boolean. You should modify it to return a count instead. Commented Apr 20, 2017 at 14:50
  • @clarkatron but what I want is to count how many times it is true but not for the entire loop just for the current and then start over...does that make sense? Commented Apr 20, 2017 at 14:57

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

Comments

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";
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.