将来自不同foreach循环的值组合到一个数组中

时间:2016-12-06 07:14:43

标签: php arrays

我正在尝试将两个foreach()循环值组合到一个数组中,并将其显示在一个表中。它们都具有相同的字段,但是如何在单个表中显示它而不是具有两个不同的表?

<tr>
   <th> ID </th>
   <th> Transaction Type </th>
</tr>
<tbody>
   <?php 
      $ch = new CommissionHelper($rep);
      $data = $ch->getTransactionRecords($member);
        foreach ($data as $record ) {
      $d = ((object)$record);
      $pt = round($d->point, 2);
      ?>
   <tr>
      <td><?php echo $d->id;?></td>
      <td><?php echo $d->type;?></td>
   </tr>
   <?php
      }
      ?>
</tbody>
<tr>
   <th> ID </th>
   <th> Transaction Type </th>
</tr>
<tbody>
   <?php 
      $query = "SELECT * FROM invoice_withdraw_debit_card where user_ID like $id";
      $data = $MySQLi_CON->query($query);
      foreach ($data as $key ) {      
      ?>
   <tr>
      <td><?php echo $key['invoice_ID'];?></td>
      <td><?php echo $key['type'];?></td>
   </tr>
   <?php
      }
      ?>
</tbody>

1 个答案:

答案 0 :(得分:1)

您可以像下面这样实现: -

<tr>
   <th> ID </th>
   <th> Transaction Type </th>
</tr>
<tbody>
   <?php 
      $ch = new CommissionHelper($rep);
      $data = $ch->getTransactionRecords($member);
        foreach ($data as $record ) {
      $d = ((object)$record);
      $pt = round($d->point, 2);
      ?>
   <tr>
      <td><?php echo $d->id;?></td>
      <td><?php echo $d->type;?></td>
   </tr>
   <?php
      }
      ?>
   <?php 
      $query = "SELECT * FROM invoice_withdraw_debit_card where user_ID like $id";
      $data = $MySQLi_CON->query($query);
      foreach ($data as $key ) {      
      ?>
   <tr>
      <td><?php echo $key['invoice_ID'];?></td>
      <td><?php echo $key['type'];?></td>
   </tr>
   <?php
      }
      ?>
</tbody>

注意: - 从代码中间删除</tbody><tr><th> ID </th><th> Transaction Type </th></tr><tbody>