for循环不处理数组的第一个元素

Jac*_*cob 1 php arrays for-loop

我对我的代码中的一个问题感到困惑,并希望其他人可以帮助我解释为什么我的循环省略(array[0])了数组的第一个元素.

代码

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

            }

        }
}
Run Code Online (Sandbox Code Playgroud)

问题

我似乎无法理解为什么但是for循环或IF语句(我不是100%确定哪一个)无法通过数组[0]的第一个元素运行.

它从数组[1]开始运行良好但是即使我已经测试了$x确实=到$i并因此测试3至少应该工作,循环似乎运行一个循环超过所有IF's,然后从数组[1]开始工作.

我试过了什么

  • 我已经改变了for ($i=1; $i<$val; $i++),从一开始就可以正常工作(例如,不会遗漏任何东西)(但当然不能解决我的问题,因为我仍然缺少数组[0])
  • 我已经在代码中测试过,如果echo在代码$val[0]的开头打印出来并且确实如此
  • 我也测试了$x = $i,这些也有效

在我的代码中改变所有东西感觉太傻了但是在整个堆栈溢出和谷歌搜索了类似的问题,这是其中一个问题,我似乎无法找出原因.

一定有什么不对的我在编写循环的方式中看不到?

Joh*_*ica 6

使用$x == $i测试相等,不$x = $i,这是一个任务.