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?