douchun6221 2017-04-20 14:31
浏览 39
已采纳

PHP计算循环的每次迭代中存在多少次值

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条回答 默认 最新

  • douzhaolu4839 2017-04-20 15:18
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 交替优化波束形成和ris反射角使保密速率最大化
  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程