【问题标题】:Want to create array with sub arrays so I can loop through JSON想用子数组创建数组,这样我就可以遍历 JSON
【发布时间】:2021-05-22 05:20:13
【问题描述】:

我正在尝试创建子数组,对此我很陌生。我的术语对我来说不是很好。我正在使用当前的 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);

下面的代码似乎创建了一个数组?与子数组?但是,手段:不给我一个数组编号吗?我想这不允许我浏览度假村?

SSR 下的所有内容都有 [0]、[1]、[2] 等。但是,一切都在使用:SSR、VGF 等都没有。我将使用循环来生成 HTML,但我似乎无法循环度假村部分。

{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 不正确,但它让我知道我在看什么。我目前在这里没有初始的“resort”循环。

$(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);
      }
});
});

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

【问题讨论】:

    标签: javascript php arrays json


    【解决方案1】:

    带有子数组的数组

    <?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);
    

    【讨论】:

    • $data = array('resort' => $row['resortname'], 'resortcode' => $row['resortcode'] => array('room' => $row[ 'roomname'], 'view' => $row['viewname'], 'checkin' => $checkin, 'checkout' => $checkout ) ) 返回:解析错误:语法错误,意外标记 "=>",期待“)”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-05
    相关资源
    最近更新 更多