Two foreachs php - Duplicate data

1

I have two selects that return two arrays and I need to go through them in the same table, but the records are being duplicated. How can I best resolve this issue?

Follow the example:

 <?php
                         foreach ($contratantes as $contratante) {
                             foreach ($contratados as $contratado) {
                                 ?>
                                 <tr>
                                     <td><?= $contratante['dataContratacao'] ?></td>
                                     <td><?= $contratante['anuncioContratante'] ?></td>
                                     <td><?= $contratante['tipoAnuncio'] ?></td>
                                     <td><?= $contratado['anuncioContratado'] ?></td>
                                     <td><?= $contratado['tipoAnuncioContratado'] ?></td>
                                     <td><?= $contratado['login'] ?></td>
                                     <td>Opções</td>


                                 </tr>

                             <?php
                             }
                         }
                        ?>
    
asked by anonymous 26.11.2016 / 03:18

1 answer

1

One of the most practical ways to change your code is to use a single loop, and use the index in the original arrays :

<?php
    $count = count($contratantes);
    for($i = 0; $i < $count; ++$i ) {
?>
      <tr>
          <td><?= $contratantes[$i]['dataContratacao'] ?></td>
          <td><?= $contratantes[$i]['anuncioContratante'] ?></td>
          <td><?= $contratantes[$i]['tipoAnuncio'] ?></td>
          <td><?= $contratados[$i]['anuncioContratado'] ?></td>
          <td><?= $contratados[$i]['tipoAnuncioContratado'] ?></td>
          <td><?= $contratados[$i]['login'] ?></td>
          <td>Opções</td>
      </tr>
<?php
    }
?>

I'm assuming that the sizes of arrays are the same.

    
26.11.2016 / 03:26