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 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试