【问题标题】:Array multidimensional to html table数组多维到html表
【发布时间】:2016-01-13 07:50:57
【问题描述】:

我知道这个问题是重复的,但我不能按照我的情况的例子。我正在查询 Quake 2 服务器的个人项目

 foreach ($results as $data) {
         echo "<pre>";
         print_r($data['players']);
    }

获得的信息:

[0] => Array
    (
        [frags] => 7
        [ping] => 28
        [nick] => Player
        [gq_name] => Player
        [gq_score] => 7
        [gq_ping] => 28
    )

[1] => Array
    (
        [frags] => 27
        [ping] => 31
        [nick] => lE'Heineken.
        [gq_name] => lE'Heineken.
        [gq_score] => 27
        [gq_ping] => 31
    )

)

那么我怎样才能在一个简单的表格中移动这些信息呢?

<table>
<tr>
    <th>Players</th>
    <th>Frags</th>
    <th>Ping</th>
</tr>
<tr>
    <td></td>
    <td></td>
    <td></td>
</tr>

我正在尝试做类似q2servers的事情

感谢您的宝贵时间,对不起我的英语。问候

【问题讨论】:

标签: php html arrays multidimensional-array


【解决方案1】:

那就试试吧,

 <table>
    <tr>
        <th>Players</th>
        <th>Frags</th>
        <th>Ping</th>
    </tr>
    <?php foreach ($results as $data) {
     <?php foreach($data['players'] as $item){ ?>
        <tr>
           <td><?php echo $item['gq_name']; ?></td>
           <td><?php echo $item['gq_frags']; ?></td>
           <td><?php echo $item['ping']; ?></td>

        </tr>
   <?php } ?>
    <?php } ?>
    </table>

【讨论】:

    【解决方案2】:

    我真的不是你需要的,但如果我理解得很好,你需要的是:

    <table>
    <tr>
        <th>Players</th>
        <th>Frags</th>
        <th>Ping</th>
    </tr>
    <?php foreach ($results['players'] as $data) {
    <tr>
        <td><?php echo $data['gq_name']; ?></td>
        <td><?php echo $data['gq_frags']; ?></td>
        <td><?php echo $data['ping']; ?></td>
    
    </tr>
    <?php } ?>
    </table>
    

    【讨论】:

    • 我将此示例应用于数据服务器信息(主机名、地图名、玩家数量)并且工作正常,但没有显示玩家信息,表格单元格为空,有什么想法吗?跨度>
    • 尝试将 $results 更改为 $data,将 $results['players'] 更改为 $data
    • Players Frags Ping
      警告:在第 118 行的 C:\wamp\www\test\new.php 中为 foreach() 提供的参数无效我不知道是什么错了
    • 如果你把 $results['players'][0] as $data
    • “警告:在第 97 行的 C:\wamp\www\test\new.php 中为 foreach() 提供的参数无效”以及来自 print_r 的信息:Array ([0] => Array ( [frags] => 0 [ping] => 5 [nick] => WallFly[BZZZ] [gq_name] => WallFly[BZZZ] [gq_score] => 0 [gq_ping] => 5 ) ) (只有一名玩家现在在线)
    【解决方案3】:

    我认为你需要这个:

    echo "
    <table>
    <tr>
        <th>Players</th>
        <th>Frags</th>
        <th>Ping</th>
    </tr>";
    
    foreach ($results as $data) {
        // print_r($data['players']);
        echo "
        <tr>
            <th>".$data['players']['nick']."</th>
            <th>".$data['players']['frags']."</th>
            <th>".$data['players']['ping']."</th>
        </tr>"
    }
    
    echo "</table>";
    

    【讨论】:

    • 单元格为空,无法显示信息,但“print_r”可以,我不知道我错过了什么
    猜你喜欢
    • 2021-09-06
    • 2020-05-27
    • 2021-04-14
    • 2014-09-12
    • 2023-03-30
    • 2019-12-14
    • 1970-01-01
    • 2011-01-28
    • 2014-05-17
    相关资源
    最近更新 更多