【问题标题】:how can i echo contents of 2 associative arrays into one table?如何将 2 个关联数组的内容回显到一张表中?
【发布时间】:2014-11-28 15:43:35
【问题描述】:

这是我的代码 sn-ps,结果不是我想要的。我有 2 个关联数组,想将它们的内容回显到一个表中,就像在 html 标记之前一样。我该怎么做?

    team A:                      team B:
    player_one                   player_five
    player_two                   player_six
    player_three                 player_seven
    player_four                  player_eight



<html>
<head>

<title>Untitled</title>
</head>
<body>
  <table widht='100%' border="1">
    <tr>
        <td>team A</td>
        <td>team B</td>

    </tr>

<?php

$team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
$team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");



foreach($team_a as $index1 => $value1 ){

foreach($team_b as $index2 => $value2 )
 echo "
            <tr>
                <td>$value1</td>
                <td>$value2</td>

            </tr>
            ";
            }

?>

</table>
</body>
</html>     

【问题讨论】:

    标签: php arrays foreach associative-array


    【解决方案1】:

    或者,您可以先组合数组并相应地排列它们,然后打印它们:

    <?php
    
    $team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
    $team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");
    
    $teams = array();
    $keys = array_keys($team_a);
    foreach ($keys as $key) {
        $teams[$key] = array($team_a[$key], $team_b[$key]);
    }
    ?>
    <table widht='100%' border="1">
    <tr>
        <td>team A</td>
        <td>team B</td>
    </tr>
    <?php foreach($teams as $players): ?>
        <tr>
        <?php foreach($players as $player): ?><td><?php echo $player; ?></td><?php endforeach; ?>
        </tr>
    <?php endforeach; ?>
    </table>
    

    Output

    或者,如果您不想触及原始数组或使用新数组,则只需相应地循环它们:

    <?php
    
    $team_a=array("goalkeeper"=>"player_one","defender"=>"player_two","midfielder"=>"player_three","forward"=>"player_four");
    $team_b=array("goalkeeper"=>"player_five","defender"=>"player_six","midfielder"=>"player_seven","forward"=>"player_eight");
    
    ?>
    <table widht='100%' border="1">
    <tr>
        <td>team A</td>
        <td>team B</td>
    </tr>
    <?php foreach($team_a as $key => $value): ?>
        <tr>
            <td><?php echo $value; ?></td><td><?php echo $team_b[$key]; ?></td>
        </tr>
    <?php endforeach; ?>
    </table>
    

    【讨论】:

      【解决方案2】:
      // drop indexes because you don't use them anyway
      $a = array_values($team_a);
      $b = array_values($team_b);
      
      // process arrays
      max = max(count($a), count($b));
      for ($i=0; $i < $max; $i++) {
          echo '
          <tr>
              <td>' . (isset($a[$i] ? $a[$i] : '-')) . '</td>
              <td>' . (isset($b[$i] ? $b[$i] : '-')) . '</td>
          </tr>
          ';
      }
      

      【讨论】:

        猜你喜欢
        • 2019-09-21
        • 2016-03-19
        • 1970-01-01
        • 2011-01-21
        • 2010-09-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多