Search code examples
phparraysloopsnested-loops

Looping Througn an Array


I have the following array named $entries:

array ( 
  0 => array ( 'entry_id' => 8132, 'racer_id' => 1302, 'class_id' => 20, ), 
  1 => array ( 'entry_id' => 8239, 'racer_id' => 851, 'class_id' => 20, ), 
  2 => array ( 'entry_id' => 8240, 'racer_id' => 850, 'class_id' => 20, ), 
  3 => array ( 'entry_id' => 8241, 'racer_id' => 1222, 'class_id' => 20, ), 
  4 => array ( 'entry_id' => 8243, 'racer_id' => 1221, 'class_id' => 20, ), 
  5 => array ( 'entry_id' => 8250, 'racer_id' => 673, 'class_id' => 20, ), 
  6 => array ( 'entry_id' => 8255, 'racer_id' => 674, 'class_id' => 20, ), 
  7 => array ( 'entry_id' => 8258, 'racer_id' => 666, 'class_id' => 20, ), 
  8 => array ( 'entry_id' => 8266, 'racer_id' => 193, 'class_id' => 20, ), 
)

and this code to loop thru that array.

foreach ($entries as $entry) {
    for ($i = 1; $i <= $list[0]['heat_count']; ++$i) {
       if ($entry['class_id'] == $class) {
           $heats[$class] = [
               'heat_nbr'   => $i,
               'entry_id'   => $entry['entry_id'],
               'racer_name' => $f->getRacerFullName($entry['racer_id'])
           ];
       }
    }
}

I am not getting the output I need. The variable $list[0]['heat_count'] comes from an earlier query that counts the amount of riders in the "heat".

What I need is to set the "heat_number" according to the "heat_count", so if there are 2 heats, I need to iterate thru the $entries array and take the 1st to heat 1 and the second to heat 2 and so on thru the loop. If the "heat_count" is 3, then 1st to 1, 2nd to 2, 3rd to 3, 4th to 1, 5th to 2 6th to 3.. and so on.

There can be any number of heats per class in an event from 1 to 50. So hard coding that is impossible without a million if statements.

Can anyone understand what I'm getting at here and push me in the right direction?

This is what I get from the above loop now:

array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8132) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8239) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8240) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8241) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8243) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8250) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8255) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8258) } array(2) { ["heat_nbr"]=> int(2) ["entry_id"]=> int(8266) } 

Solution

  • You need to keep track of how many entrants you have assigned to the heats, so you can assign the next heat number.

    Something like this should work:

    $numberOfHeats = $list[0]['heat_count'];
    $counter = 0;
    
    foreach ($entries as $entry){
        if($entry['class_id'] == $class) {
    
            $nextHeatNumberToAssign = ($counter % $numberOfHeats) + 1;
            $counter++;
    
            $heats[$class][] = [
                'heat_nbr'   => $nextHeatNumberToAssign,
                'entry_id'   => $entry['entry_id'],
                'racer_name' => $f->getRacerFullName($entry['racer_id'])
                ];
        }
    }