1

I'm able to iterate over a PHP array using foreach, but not with a for loop:

this does not work:

for($j = 0; $j < sizeof($jobsFilterA); $j++) {
    echo $j . $jobsFilterA[$j] . "<br>";
}

this does:

foreach($jobsFilterA as $job) {
    echo $job . "<br>";
}

I've got 5 different arrays, some of which were simply built from database extract, others built then trimmed by removing specific indices.

Getting the feeling I've just got empty indices due to use of the unset function now that I type this all out.

Can I remove unset indices or how would be the proper way of rebuilding the array?

Thanks in advance!

Tried iterating over multiple different arrays in different ways.

Code is above.

Unset indices in the for loop throw an undefined offset exception/error.

In the end, these 5 arrays need to be echoed into a table. I've been doing that via a for loop like so:

/*
*                   Echo data into a table
*/
echo '<tbody>';
for($i=0; $i < sizeof($jobsFilterA); $i++){
    echo "<tr><td>" . strtoupper(substr($custLastNames[$i], 0, 4)) . "</td>";
    echo "<td width='100'>" . $jobsFilterA[$i] . "</td>";
    echo "<td>" . $waxDueDates[$i] . "</td>";
    echo "<td>" . $dayDifferences[$i] . "</td>";
    echo "<td>" . $revisedETAs[$i] . "</td></tr>";
}
echo '</tbody>'

which is why I'm hoping I can reorganize the arrays so that their data is indexed in alignment/with continuity.

Mikey
  • 107
  • 2
  • 8
  • 1
    If you've used `unset` on a numeric sequence except in the case of unseting the last index, then the `for` will fail as you'll be accessing a key that no longer exists (and will be getting a warning). E.g. if you have an array `['a', 'b', 'c', 'd']` and you unset index 2, you end up with `[0 => 'a', 1 => 'b', 3 => 'd']`, but your `for`loop doesn't know that, so your code still checks `$jobsFilterA[2]` when $j is 2. If you really want to use a `for` you can reset the keys using e.g. [array_values](http://php.net/array_values). – Jonnix Sep 03 '19 at 00:18
  • 1
    Another option if you just want to check the same indexes that are in `$jobsFilterA` in the other arrays is `foreach($jobsFilterA as $i => $job)` then you can access e.g. `$waxDueDates[$i]` where $i is the same index as the current iteration of `$jobsFilterA`. There are various other ways of doing this too. – Jonnix Sep 03 '19 at 00:24
  • @Jonnix those two comments combined are pretty much the answer. You should post... – Nick Sep 03 '19 at 00:31
  • Good point. Done. – Jonnix Sep 03 '19 at 00:38

1 Answers1

3

If you've used unset on a numeric sequence (except in the case of un-setting the last index), then the for will fail as you'll be accessing a key that no longer exists (and will be getting a warning).

E.g. if you have an array ['a', 'b', 'c', 'd'] and you unset index 2, you end up with [0 => 'a', 1 => 'b', 3 => 'd'], but your for loop doesn't know that, so your code still checks $jobsFilterA[2] when $j is 2. If you really want to use a for, you can reset the keys using e.g. array_values which would give you [0 => 'a', 1 => 'b', 2 => 'd'] which your existing for will work with.

Another option if you just want to check the same indexes that are in $jobsFilterA in the other arrays is to use foreach($jobsFilterA as $i => $job) then you can access e.g. $waxDueDates[$i] where $i is the same index as the current iteration of $jobsFilterA. There are various other ways of doing this too.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Jonnix
  • 4,121
  • 1
  • 30
  • 31