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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用