我正在尝试将JSON输出转换为HTML表并使用AJAX调用它,但由于多维数组,我没有得到正确的HTML表。 以下是我得到的输出。
PHP代码:
<?php
$res = file_get_contents(
'https://sphirelabs-indian-telecom-data-recharge-plans-v1.p.mashape.com/' .
'telecomdata/v1/get/index.php?circle=tn&opcode=idea&type=Topup',
false,
$context
);
$data = json_decode($res, true);
if (count($data) > 0):
?>
<table>
<thead>
<tr>
<th><?= implode('</th><th>', array_keys(current($data))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($data as $row): ?>
<tr>
<?php foreach ($row as $item): ?>
<td><?= $item ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
AJAX OUTPUT:
0 1 2 3 4 5 6 7 8 9 10 11 12
Array Array Array Array Array Array Array Array Array
JSON回复:
{
"data": [
{
"id": "2602",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "10",
"recharge_talktime": "6.9",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 10 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
},
{
"id": "2603",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "20",
"recharge_talktime": "14.8",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 20 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
},
{
"id": "2604",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "30",
"recharge_talktime": "23.7",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 30 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
},
{
"id": "2605",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "40",
"recharge_talktime": "32.6",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 40 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
},
{
"id": "2606",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "50",
"recharge_talktime": "41.5",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 50 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
},
{
"id": "2607",
"operatorid": "T24",
"circleid": "UP West",
"recharge_amount": "100",
"recharge_talktime": "86",
"recharge_validity": "NA",
"recharge_shortdesc": "Recharge of Rs 100 By T24",
"recharge_longdesc": "Regular Recharge ",
"recharge_type": "Topup"
}
]
}