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条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮