douzhouqin6223 2014-10-03 14:55
浏览 25
已采纳

如何将2个关联数组的内容回显到一个表中?

this is my code snippets and the result is not the thing that i want.I have 2 associative arrays and want to echo their contents into one table like before the html tag.how can i do that?

    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>     
  • 写回答

2条回答 默认 最新

  • dongxuying7583 2014-10-03 15:00
    关注

    Alternatively, you could combine the arrays first and line them up accordingly, them print them:

    <?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

    Or if you do not want the original arrays touched or using a new array, then just loop them accordingly:

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?