【问题标题】:Combine values from different foreach loop into an array将来自不同 foreach 循环的值组合成一个数组
【发布时间】:2016-12-06 15:22:12
【问题描述】:

我正在尝试将两个 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>

【问题讨论】:

    标签: php arrays


    【解决方案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>
    

    注意:- 从代码中间删除 &lt;/tbody&gt;&lt;tr&gt;&lt;th&gt; ID &lt;/th&gt;&lt;th&gt; Transaction Type &lt;/th&gt;&lt;/tr&gt;&lt;tbody&gt;

    【讨论】:

    • fypforstack 很高兴为您提供帮助。干杯:):):)
    猜你喜欢
    • 2016-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多