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

使用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>

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>

Put your <p> inside the inner loop:

foreach(...) {
   foreach(... as $foo) {
       echo "<p>$foo</p>";
   }
}

Since they're OUTSIDE the inner loop, you only get one <p> set, instead of one per inner-item.

I think you can avoid the second foreach like so:

$tablesubheading = rwmb_meta( 'tb_table1_sub_heading', 'type=text' );
echo $tablesubheading[0];

Although I may have interpreted this completely wrong.