gpt4 book ai didi

php - 无法将 JSON 解码为 PHP 数组并显示在 HTML 表中

转载 作者:可可西里 更新时间:2023-10-31 23:51:20 24 4
gpt4 key购买 nike

我有这个 json 数据

{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1,
},
{
"id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
"name": "3B Unit",
"suite": "3B",
"sf": 450,
"unit_floor": 3,
},
{
"id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
"name": "3A Unit",
"suite": "3A",
"sf": 450,
"unit_floor": 3,
},
{
"id": "8ec4325a-1271-560e-888b-56cd6120f5de",
"name": "2B Unit",
"suite": "2B",
"sf": 450,
"unit_floor": 2,
},
{
"id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
"name": "1B Unit",
"suite": "1B",
"sf": 450,
"unit_floor": 1,
},
{
"id": "61a55092-5683-1088-2d6c-56c99c5d4873",
"name": "2A Unit",
"suite": "2A",
"sf": 3000,
"unit_floor": 2,
}
]
}

我想根据楼层数在表格中显示此数据。像三楼单元 3A、3B 及其面积总和(以平方英尺为单位)排成一排,二楼单元 2A、2B 及其面积总和排成一排,一楼单元 1A、1B 及其面积总和排成一排。请帮助我。

我试过了,但没有给出预期的结果

$unit_response = call($url, $oauth2_token_response->access_token, 'GET');

$unit_json = json_decode(json_encode($unit_response),true);
<table class="stak-plan">

<?php foreach ($unit_json['records'] as $unit_data) { ?>

<tr>
<td>Floor: 3</td>
<td>
Suit : <?php echo $unit_data['name'] ?><br>
SF : <?php echo $unit_data['sf'] ?>
</td>
<td>Suite: 3B<br /> SF: 25,000</td>
<td>Suite: 3C<br /> SF: 25,000</td>
<td>
</td>
</tr>
<?php } ?>

</table>

这是预期的输出。 Output

最佳答案

CSS:

<style>
td{
border: 1px solid black;
padding: 15px;
}
</style>

JSON(一些额外的逗号已被删除):

$json = '{
"next_offset": -1,
"records": [
{
"id": "87dad127-a60e-15a3-148e-56c7675f11df",
"name": "1A Unit",
"suite": "1A",
"sf": 1200,
"unit_floor": 1
},
{
"id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
"name": "3B Unit",
"suite": "3B",
"sf": 450,
"unit_floor": 3
},
{
"id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
"name": "3A Unit",
"suite": "3A",
"sf": 450,
"unit_floor": 3
},
{
"id": "8ec4325a-1271-560e-888b-56cd6120f5de",
"name": "2B Unit",
"suite": "2B",
"sf": 450,
"unit_floor": 2
},
{
"id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
"name": "1B Unit",
"suite": "1B",
"sf": 450,
"unit_floor": 1
},
{
"id": "61a55092-5683-1088-2d6c-56c99c5d4873",
"name": "2A Unit",
"suite": "2A",
"sf": 3000,
"unit_floor": 2
}
]
}';

PHP:

    $jd = json_decode($json);
$floors = array();
foreach ($jd->records AS $key => $obj) {
$f = $obj->unit_floor;
$id = $obj->id;
$name = $obj->name;
$suite = $obj->suite;
$sf = $obj->sf;
$floors[$f][$suite]['name'] = $name;
$floors[$f][$suite]['id'] = $id;
$floors[$f][$suite]['sf'] = $sf;
}
//sort floors in desc order
krsort($floors);
foreach($floors as $id => $floor){
ksort($floors[$id]);
}
print '<table>';
foreach($floors as $floor => $suites){
$sqf = 0;
print '<tr>';
print '<td>FLOOR: '.$floor.'</td>';
foreach($suites AS $suite => $value){
$sqf += $value['sf'];
print '<td>Suite: '.$suite.'<br>SF: '.$value['sf'].'</td>';
}
print '<td>'.$sqf.'</td>';
print '</tr>';
}
print '</table>';

输出:

enter image description here

关于php - 无法将 JSON 解码为 PHP 数组并显示在 HTML 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630054/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com