【问题标题】:How to create html table from multidimensional array in PHP?如何在 PHP 中从多维数组创建 html 表?
【发布时间】:2015-06-16 02:17:30
【问题描述】:

我正在向服务器发送 HTTP 请求并获得 json 格式的响应。我使用以下方法将其转换为数组:

$response = file_get_contents($final_url);
$responseArray = json_decode($response, true);

这里是服务器响应数组($responseArray)。

Array ( 
[status] => OK 
[result] => Array (
      [0] => Array
           (
             [trainno] => 12151
             [name] => SAMARSATA EXP
             [cls] => 1A 2A 3A SL
             [rundays] => F,Sa
             [from] => BQA
             [fromname] => Bankura
             [dep] => 03.45
             [to] => HWH
             [toname] => Howrah Jn
             [arr] => 08.25
             [pantry] => 0
             [type] => SUPERFAST
             [datefrom] => 10-Apr-2015
             [dateto] => 12-Apr-2020
             [traveltime] => 04.40
           )
      [1] => Array
           (
             [trainno] => 12151
             [name] => SAMARSATA EXP
             [cls] => 1A 2A 3A SL
             [rundays] => F,Sa
             [from] => BQA
             [fromname] => Bankura
             [dep] => 03.45
             [to] => HWH
             [toname] => Howrah Jn
             [arr] => 08.25
             [pantry] => 0
             [type] => SUPERFAST
             [datefrom] => 10-Apr-2015
             [dateto] => 12-Apr-2020
             [traveltime] => 04.40
           )
    )
)

如果 [status] => OK,现在我想以 html 表格格式打印“结果”数组。我该怎么做?

【问题讨论】:

    标签: php arrays for-loop html-table


    【解决方案1】:

    只需添加一个 if 条件,如果满足,则迭代所述数组,指向正确的索引:

    if($responseArray['status'] === 'OK') {
        foreach($responseArray['result'] as $result) {
            echo $result['name']; // and other indices
        }
    }
    

    将其打印到表格标记中只是创建一个包含标记的普通表格。

    超级粗略的例子:

    <?php if($responseArray['status'] === 'OK'): ?>
    <table>
        <thead>
            <tr>
            <?php foreach($headers as $h): ?>
                <th><?php echo $h; ?></th>
            <?php endforeach; ?>
            </tr>
        </thead>
        <tbody>
            <?php foreach($responseArray['result'] as $row): ?>
            <tr>
                <?php foreach($row as $value); ?>
                    <td><?php echo $value; ?></td>
                <?php endforeach; ?>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>
    

    【讨论】:

    • @BikashMahata 很高兴这有帮助
    • 你好幽灵。再帮我1个
    • Array ([status] => OK [result] => Array ( [pnr] => 4106121143 [cls] => 3A [ticket] => 1 [journey] => 09-Apr- 2015 [trainno] => 12740 [name] => VSKP GARIB RATH [from] => SC [to] => VSKP [brdg] => SC [passengers] => Array ( [0] => Array ( [bookingstatus] => G14 52GN [currentstatus] => Can/Mod [coach] => ) [1] => Array ( [bookingstatus] => G14 54GN [currentstatus] => Can/Mod [coach] => ) ) [chart] => 图表准备 [错误] => ) )
    • @BikashMahata 当在数组中引入另一个级别时,只需在其中添加另一个 foreach,它的工作原理相同 foreach($row['passengers'] as $val){ echo $val['bookingstatus']; }
    • 我已添加: if($responseArray['status'] === 'OK') { foreach($responseArray['result'] as $result) { echo ''; echo ''.$result['journey'].'';回声''; } }
    猜你喜欢
    • 2020-07-17
    • 2014-01-11
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2011-06-12
    • 1970-01-01
    • 1970-01-01
    • 2011-12-14
    相关资源
    最近更新 更多