想要创建带有子数组的数组,这样我就可以循环使用JSON了



我正在尝试创建子数组,对此我很陌生。我的术语不太好,所以我使用当前的PHP代码来生成JSON数组。JSON输出如下。

foreach ($result as $row) {
$points = $row['points'];
$price = $points*20;
$pricefrmt = "$".number_format ($price, 2);
$return['resort'][$row['resortcode']][] = [
'resort' => $row['resortname'],
'roomcode' => $row['roomcode'],
'room' => $row['roomname'],
'view' => $row['viewname'],
'checkin' => $checkin,
'checkout' => $checkout,
'available' => $row['available'],
'price' => $pricefrmt,
'points' => $row['points'],
'sleeps' => $row['sleeps'],
'sleepdetails' => $row['sleepdetails'],
'bedding' => $row['bedding'],
'sqfoot' => $row['sqfoot'],
'roomdescription' => $row['roomdescription'],
'amenities' => $row['amenities'],
'layoutimg' => $row['layoutimg'],
'roomimg' => $row['roomimg'],
'roomimgthumb' => $row['roomimgthumb'],
];
}
echo json_encode($return);

下面的代码似乎创建了一个数组?使用子阵列?但是,resort:不给我一个数组号吗?我想这不允许我穿过度假村吗?

SSR下的所有东西都有[0]、[1]、[2]等。然而,resort下的所有事物:SSR、VGF等没有。我将使用一个循环来生成HTML,但我似乎无法循环resort部分。

{resort: {…}}
resort:
SSR: Array(7)
0: {resort: "Saratoga Springs Resort & Spa", roomcode: "TA", room: "Deluxe Studio", view: "Standard View", checkin: "2021-06-14", …}
1: {resort: "Saratoga Springs Resort & Spa", roomcode: "S9", room: "Deluxe Studio", view: "Preferred View", checkin: "2021-06-14", …}
2: {resort: "Saratoga Springs Resort & Spa", roomcode: "TB", room: "1-Bedroom Villa", view: "Standard View", checkin: "2021-06-14", …}
3: {resort: "Saratoga Springs Resort & Spa", roomcode: "SB", room: "1-Bedroom Villa", view: "Preferred View", checkin: "2021-06-14", …}
4: {resort: "Saratoga Springs Resort & Spa", roomcode: "TL", room: "2-Bedroom Lock-Off Villa", view: "Standard View", checkin: "2021-06-14", …}
5: {resort: "Saratoga Springs Resort & Spa", roomcode: "TC", room: "2-Bedroom Villa", view: "Standard View", checkin: "2021-06-14", …}
6: {resort: "Saratoga Springs Resort & Spa", roomcode: "TP", room: "2-Bedroom Lock-Off Villa", view: "Preferred View", checkin: "2021-06-14", …}
length: 7
__proto__: Array(0)
VGF: Array(9)
0: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "86", room: "Deluxe Studio", view: "Standard View", checkin: "2021-06-14", …}
1: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "81", room: "Deluxe Studio", view: "Lake View", checkin: "2021-06-14", …}
2: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "87", room: "1-Bedroom Villa", view: "Standard View", checkin: "2021-06-14", …}
3: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "82", room: "1-Bedroom Villa", view: "Lake View", checkin: "2021-06-14", …}
4: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "89", room: "2-Bedroom Lock-Off Villa", view: "Standard View", checkin: "2021-06-14", …}
5: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "88", room: "2-Bedroom Villa", view: "Standard View", checkin: "2021-06-14", …}
6: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "84", room: "2-Bedroom Lock-Off Villa", view: "Lake View", checkin: "2021-06-14", …}
7: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "83", room: "2-Bedroom Villa", view: "Lake View", checkin: "2021-06-14", …}
8: {resort: "Disney's Grand Floridian Resort & Spa", roomcode: "85", room: "3-Bedroom Grand Villa", view: "Lake View", checkin: "2021-06-14", …}

我觉得不需要它,我知道这个javascript不正确,但它让我知道我在看什么;度假村;在这里循环。

$(function getData() {
$("#searchbtn").click(function () {
var checkin = document.getElementById("start").value;
var checkout = document.getElementById("end").value;
var occupants = document.getElementById("party").value;
$.ajax({
url: "action.php",
type: "POST",
data: { checkin: checkin, checkout: checkout, occupants: occupants },
dataType: "json",
success: function (response) {
console.log(response);
console.log(response.resort.SSR[4].roomcode);
searchresult(response);
}
});
let searchresult = function(response) {
let container = document.getElementById('resultsbox');
let output = "";

for (let j = 0; j < response.resort.SSR.length; j++) {
output +=
"<div class='roomresults'>"+
"<div>" + response.resort.SSR[j].room + " - " + response.resort.SSR[j].view + "</div>" +
"</div>";
}

$(container).html(output);
}
});
});

任何帮助都是感激的,我只是在努力学习和理解。谢谢

带子阵列的阵列

<?php $data = array(
'name'=>'ali',
'lastname'=>'ahmadi',
'emails'=> array(
'email1'=>'ali1@gmail.com',
'email2'=>'ali2@gmail.com',
'email3'=>'ali3@gmail.com',
)
)?>

到JSON

$toJSON = json_encode($data);

最新更新