Two php foreachs - Duplicate data

Asked

Viewed 183 times

1

I have two selects that return me 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
                             }
                         }
                        ?>

1 answer

1


One of the most practical ways without changing your code too much is to use a single loop, and use the index in arrays originals:

<?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 the sizes of arrays are the same.

  • So, it works when I don’t have any repeated ID, when some id repeats in another record returns this error: Undefined offset: 2

  • I had not yet edited... It was now

  • From what you put in the question, I think the problem lies elsewhere in your code. You probably want a JOIN on DB, but you’re trying to make PHP a relation instead of right on select. But I’m not sure that’s it. Anyway, it doesn’t seem right to use arrays for this.

  • If this is the case, see if it helps: http://answall.com/questions/6441/70 - if this is not the case, put the piece of code that mounts the arrays in the question as well, or preferably make a [mcve] of the problem.

  • The Join is working as it should by the bank, usually... Only to build this table, I use two methods from two joins... until then beauty, I tried to use two foreach, gave error... I did what you suggested and seemed to have solved, only when adding data in the table, some of the records in this query gives Undefined offset, it may not be for the reason I mentioned the ids

  • Undefined offset can be an array shorter than the other. You would have to see the part that mounts the arrays to know. Without seeing the excerpt of the code that Join makes and picks up the arrays, I am tied hands to help. Normally in this case I would not need two arrays. Could take from DB and already play on screen

  • 1

    Dude, I figured out the cause of the offset problem, it was an array_search I was using before interacting the array, it was a solution I was testing before asking here... I took it out and it worked as it should, with your first answer. Thanks anyway

  • When you ask a next question, try to put more details, then we can help better. At least a little more of the code that works with the problem data. I don’t mean by the whole code, if it’s too much, but usually by putting the whole section of the part involved helps not only to respond, but also makes it easier to propose certain optimizations. We’re here to help, but first we need to understand what’s going on :)

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.