dongyou7472 2015-07-03 15:47
浏览 28
已采纳

使用foreach在foreach中使用php [关闭]

So I'm using a foreach to output my table-cells and have run into a problem with my output.

This is what i'm trying to achieve:

<tr>
    <th scope="col"> <h3>Ember</h3>
        <p> Xeon E3-1231</p>
    </th>
    <th scope="col"> <h3>Ember</h3>
        <p> Xeon E3-1231</p>
    </th>
    <th scope="col"> <h3>Ember</h3>
        <p> Xeon E3-1231</p>
    </th>
    <th scope="col"> <h3>Ember</h3>
        <p> Xeon E3-1231</p>
    </th>
    <th scope="col"> <h3>Ember</h3>
        <p> Xeon E3-1231</p>
    </th>
</tr>

This is my code:

<tr>
    <?php 
    $tableheading = rwmb_meta( 'tb_table1_heading', 'type=text' );
    foreach ( $tableheading as $heading )
    { ?>
<th scope="col"> <h3><?php echo $heading; ?></h3>
<p>
<?php 
            $tablesub = rwmb_meta( 'tb_table1_sub_heading' );
            if (!empty($tablesub)){

            $tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
            foreach ( $tablesubheading as $subheading )
            { echo $subheading; } 

        } ?>
        </p>
    </th>
    <?php } ?>
</tr>

Which is giving me this:

<tr>
  <th scope="col">
    <h3>Ember</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>
  <th scope="col">
    <h3>Ember2</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>
  <th scope="col">
    <h3>Ember3</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>
  <th scope="col">
    <h3>Ember4</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>
  <th scope="col">
    <h3>Ember5</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>
  <th scope="col">
    <h3>Ember6</h3>
    <p>
      Xeon E3-1231v3Xeon E3-1231v3 2Xeon E3-1231v3 3Xeon E3-1231v3 4Xeon E3-1231v3 5Xeon E3-1231v3 6</p>
  </th>

</tr>

</div>
  • 写回答

3条回答 默认 最新

  • douxu5233 2015-07-03 16:00
    关注

    You probably want something along the lines of this, if your array keys are the same:

    foreach( $tableheading as $index => $heading ) {
       echo $tablesubheading[$index];
    }
    

    It would be easier if you could integrate the $tablesubheading table into the $tableheading, though. Of course, I'm not sure if their keys are the same!

    If they are, then in your example this would be:

    <tr>
        <?php 
            $tableheading = rwmb_meta( 'tb_table1_heading', 'type=text' );
            foreach ( $tableheading as $index => $heading ) { 
        ?>
        <th scope="col"> <h3><?php echo $heading; ?></h3>
        <p>
            <?php 
                    $tablesub = rwmb_meta( 'tb_table1_sub_heading' );
                    if (!empty($tablesub)) {
                        $tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
                        echo $tablesubheading[$index];
                    } 
                    ?>
                </p>
            </th>
        <?php 
            } 
        ?>
    </tr>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 平板录音机录音问题解决
  • ¥15 请问维特智能的安卓APP在手机上存储传感器数据后,如何找到它的存储路径?
  • ¥15 (SQL语句|查询结果翻了4倍)
  • ¥15 Odoo17操作下面代码的模块时出现没有'读取'来访问
  • ¥50 .net core 并发调用接口问题
  • ¥15 网上各种方法试过了,pip还是无法使用
  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启