dqingn8836 2016-06-27 12:19
浏览 103
已采纳

for循环不处理数组的第一个元素[重复]

This question already has an answer here:

I am puzzled by an issue in my code and hoping someone else can help me shed some light on why my loop is omitting the first element (array[0]) of the array.

the Code

foreach ($a as $key => $val) {

        for ($i=0; $i<count($val); $i++) {
            $x = $i; //this helps me jump logical arguments without the use of else


            // First Test
            if (isset($val[$i+2]) && $x = $i) {

            //Do a bunch of stuff

                    if (isset(the stuff done above)) {
                    // do other things and reset $i to jump through the array
                    $i=$i+2;

                    }                 

                    else {
                        unset($things);
                        unset($otherthings);
                    }
                }
            }


            // Second Test
            if (isset($val[$i+1]) && $x = $i) {

            //Do a bunch of stuff

                    if (isset(the stuff done above)) {
                    // do other things and reset $i to jump through the array
                    $i=$i+1;

                    }                 

                    else {
                        unset($things);
                        unset($otherthings);
                    }
                }
            }

            // Third and final test
            if ($x = $i) {

               //do other things

            }

        }
}

the Problem

I can't seem to understand why but the for loop or the IF statements (I am not 100% sure which one) fail to run through the first element of the array[0].

It runs fine from array[1] onward but even though i have tested that $x is indeed = to $i and therefore test 3 at the very least should work, the loop seems to run one loop past all the IF's and then start working from array[1].

What I have tried

  • I have changed for ($i=1; $i<$val; $i++) and this works fine (e.g. does not omit anything) from the start (but of course does not solve my problem as I am still missing array[0])
  • I have tested in the code if echo $val[0] prints out at the beginning of the code and it does
  • I have also tested $x = $i and these also work

It is one of those issues that feel too silly to change everything in my code but having searched a lot throughout stack overflow and google for similar issues, I cannot seem to find out why.

There must be something wrong I cannot see in the way I have written the loop?

</div>
  • 写回答

1条回答 默认 最新

  • douyou2368 2016-06-27 12:21
    关注

    Use $x == $i to test for equality, not $x = $i, which is an assignment.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 关于#python#的问题:自动化测试