如何从 PHP 中的多维数组创建 html table?
How to create html table from multidimensional array in PHP?
我正在向服务器发送 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 table 格式打印 'result' 数组。我该怎么做?
只需添加一个if条件,如果满足,则迭代所述数组,指向适当的索引:
if($responseArray['status'] === 'OK') {
foreach($responseArray['result'] as $result) {
echo $result['name']; // and other indices
}
}
将其打印到 table 标记中只是创建一个包含标记的普通 table。
超级粗略的例子:
<?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; ?>
我正在向服务器发送 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 table 格式打印 'result' 数组。我该怎么做?
只需添加一个if条件,如果满足,则迭代所述数组,指向适当的索引:
if($responseArray['status'] === 'OK') {
foreach($responseArray['result'] as $result) {
echo $result['name']; // and other indices
}
}
将其打印到 table 标记中只是创建一个包含标记的普通 table。
超级粗略的例子:
<?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; ?>