访问顶级嵌套Json

时间:2019-08-22 14:08:44

标签: php arrays json

我有一个大的JSON文件,当更新等时,该数字将随着时间的推移而逐渐变化。例如,每辆车用一个数字表示,第一个数字从[0]到[159]开始。使用下面的代码,我可以从各种汽车中访问各个零件。感谢评论的帮助。

我无法以相同的方式从JSON获取图像URI。我收到以下错误消息:Notice: Undefined index:

echo $car['Images']['ImageURI'];  

任何帮助将不胜感激!

$response = json_decode($json_data, true);
            foreach ($response as $index => $car) {
                $car['BasicPrice'];
                $car['BodyStyle']['BodyStyle'];
                $car['Colour'];
                $car['EngineNumber'];
                $car['EngineSize'];
                $car['FuelType']['FuelType'];
                $car['HasServiceHistory'];
                $car['IsNew'];
                $car['InternetPrice'];
                $car['IsReserved'];
                $car['Make']['Make'];
                $car['Model']['Model'];
                $car['Odometer'];
                $car['PhotoCount'];
                $car['PreviousOwners'];
                $car['RegistrationNumber'];
                $car['RetailPrice'];
                $car['Specification']['Doors'];
                $car['StockNumber'];
                $car['Transmission']['Transmission'];

            }

            if (!is_array($response) || empty($response)) {
                return false;
            }

这是第一辆汽车[0],并且紧随其后。

array(159){
  [
    0
  ]=>array(42){
    [
      "BasicPrice"
    ]=>float(4995.83)[
      "BodyStyle"
    ]=>array(2){
      [
        "BodyStyle"
      ]=>string(4)"3 DR"[
        "BodyStyle_UID"
      ]=>string(36)"4cf50fe2-8c3f-e611-80cb-0a94ef0355af"
    }[
      "Colour"
    ]=>string(12)"Red 'n' Roll"[
      "DaysInStock"
    ]=>int(1021)[
      "DaysInStockGroup"
    ]=>int(1021)[
      "Discount"
    ]=>float(0)[
      "EngineNumber"
    ]=>string(15)"B12XER-19MV2339"[
      "EngineSize"
    ]=>int(1229)[
      "EstimatedDeliveryDate"
    ]=>string(19)"2016-11-04T00:00:00"[
      "FuelType"
    ]=>array(2){
      [
        "FuelType"
      ]=>string(6)"Petrol"[
        "FuelType_UID"
      ]=>string(36)"9cd0d601-bb1f-409d-b04d-4255ff47dc85"
    }[
      "HasServiceHistory"
    ]=>bool(false)
    "Images": [
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 1,
                "Image_UID": "2088e32e-d46a-42c2-8412-3802743bcc06",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=1&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 2,
                "Image_UID": "590cdfa2-d791-4414-bbb4-e5fbe7a5ac80",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=2&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 3,
                "Image_UID": "038674d1-f32e-40ac-90bf-fad4d3b25e7f",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=3&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 4,
                "Image_UID": "9e4d36b0-7e9d-4159-9315-548e30835aa4",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=4&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 5,
                "Image_UID": "c3ad8b52-df16-4925-b8f2-6434a1d3ece6",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=5&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 6,
                "Image_UID": "b5e66c41-a76e-4cd2-a535-623715e19961",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=6&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 7,
                "Image_UID": "6ce8cb63-4cee-4451-abd6-abc367d9f1d2",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=7&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 8,
                "Image_UID": "7b5ecc82-b8ea-4af4-85d6-1ab13c527edf",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=8&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 9,
                "Image_UID": "a1b2dae3-342f-4917-b7d8-6d9135bb8966",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=9&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 10,
                "Image_UID": "a6e1fce8-7bf9-4570-898b-91aa368c889d",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=10&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 11,
                "Image_UID": "0587afe6-4d95-4ce3-b571-0c1bc894516f",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=11&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 12,
                "Image_UID": "5b4713c8-37c9-4702-937c-4938c95bbee9",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=12&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 13,
                "Image_UID": "6c6a83d9-0782-42c6-a1fe-2d14272c7832",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=13&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 14,
                "Image_UID": "6e8a2171-e85a-4afd-a4fb-e70eeab38d10",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=14&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 15,
                "Image_UID": "f60e84ab-91bf-494d-9a54-0ad27615135a",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=15&Width=960",
                "ImageWidth": 960
            },
            {
                "Entity_UID": "8be4d43d-e3a4-e611-80cf-0a94ef0355af",
                "ImageNumber": 16,
                "Image_UID": "bfe311ca-4a62-4518-b27f-4d1473386741",
                "ImageURI": "https://vehiclestock-public.pinnacledms.net/ViewVehiclePhoto.aspx?BUID=a3db2a66-b4fb-4ac2-a78a-0f042aab50af&VUID=8be4d43d-e3a4-e611-80cf-0a94ef0355af&Rank=16&Width=960",
                "ImageWidth": 960
            }[
      "Interior"
    ]=>array(2){
      [
        "Interior"
      ]=>string(0)""[
        "Interior_UID"
      ]=>string(36)"05f50fe2-8c3f-e611-80cb-0a94ef0355af"
    }[
      "IsNew"
    ]=>bool(false)[
      "InternetPrice"
    ]=>float(0)[
      "IsQualifying"
    ]=>bool(true)[
      "IsReserved"
    ]=>bool(false)[
      "Location"
    ]=>string(9)"Autospray"[
      "Make"
    ]=>array(3){
      [
        "FranchiseModel"
      ]=>NULL[
        "Make"
      ]=>string(8)"Vauxhall"[
        "Make_UID"
      ]=>string(36)"ac84ccae-882d-e611-80ca-0a94ef0355af"
    }[
      "ManufacturerRecommendedRetailPrice"
    ]=>float(0)[
      "Model"
    ]=>array(3){
      [
        "Make_UID"
      ]=>string(36)"ac84ccae-882d-e611-80ca-0a94ef0355af"[
        "Model"
      ]=>string(4)"Adam"[
        "Model_UID"
      ]=>string(36)"cb34d5c0-5c3f-e611-80cb-0a94ef0355af"
    }[
      "Odometer"
    ]=>int(13582)[
      "Options"
    ]=>array(0){

    }[
      "OrganisationalUnit"
    ]=>array(6){
      [
        "Address"
      ]=>array(7){
        [
          "Address1"
        ]=>string(17)"Blackhouse Circle"[
          "Address2"
        ]=>string(28)"Blackhouse Industrial Estate"[
          "Address3"
        ]=>string(0)""[
          "Country"
        ]=>NULL[
          "County"
        ]=>string(8)"Aberdeen"[
          "PostCode"
        ]=>string(8)"AB42 1BN"[
          "PostTown"
        ]=>string(9)"Peterhead"
      }[
        "Description"
      ]=>string(22)"Gillanders Motors Ltd."[
        "EmailAddress"
      ]=>string(22)"email@gillanders.co.uk"[
        "OrganisationalUnit_UID"
      ]=>string(36)"a3db2a66-b4fb-4ac2-a78a-0f042aab50af"[
        "ParentOrganisationalUnit_UID"
      ]=>string(36)"51a9312b-1462-4b7c-aee3-319d6d86ecec"[
        "Telephone"
      ]=>string(12)"01779 479777"
    }[
      "PhotoCount"
    ]=>int(16)[
      "PhysicalLocation"
    ]=>array(5){
      [
        "DealerCode"
      ]=>string(0)""[
        "Description"
      ]=>string(22)"Gillanders Motors Ltd."[
        "LocationDescription"
      ]=>string(9)"Autospray"[
        "OrganisationalUnit"
      ]=>array(6){
        [
          "Address"
        ]=>array(7){
          [
            "Address1"
          ]=>string(17)"Blackhouse Circle"[
            "Address2"
          ]=>string(28)"Blackhouse Industrial Estate"[
            "Address3"
          ]=>string(0)""[
            "Country"
          ]=>NULL[
            "County"
          ]=>string(8)"Aberdeen"[
            "PostCode"
          ]=>string(8)"AB42 1BN"[
            "PostTown"
          ]=>string(9)"Peterhead"
        }[
          "Description"
        ]=>string(22)"Gillanders Motors Ltd."[
          "EmailAddress"
        ]=>string(22)"email@gillanders.co.uk"[
          "OrganisationalUnit_UID"
        ]=>string(36)"a3db2a66-b4fb-4ac2-a78a-0f042aab50af"[
          "ParentOrganisationalUnit_UID"
        ]=>string(36)"51a9312b-1462-4b7c-aee3-319d6d86ecec"[
          "Telephone"
        ]=>string(12)"01779 479777"
      }[
        "UID"
      ]=>string(36)"a3db2a66-b4fb-4ac2-a78a-0f042aab50af"
    }[
      "PreviousOwners"
    ]=>int(0)[
      "Profiles"
    ]=>array(1){
      [
        0
      ]=>array(2){
        [
          "Description"
        ]=>string(14)"Long Term Loan"[
          "UID"
        ]=>string(36)"fe8b7140-87e0-4784-88b1-d607865be492"
      }
    }[
      "RegistrationDate"
    ]=>string(19)"2016-10-31T00:00:00"[
      "RegistrationNumber"
    ]=>string(8)"SW66 EZH"[
      "RetailPrice"
    ]=>float(5995)[
      "Specification"
    ]=>array(3){
      [
        "Doors"
      ]=>int(0)[
        "Specification"
      ]=>string(3)"Jam"[
        "Specification_UID"
      ]=>string(36)"8795a7e0-e1a4-e611-80cf-0a94ef0355af"
    }[
      "Status"
    ]=>int(0)[
      "StockNumber"
    ]=>string(5)"U1211"[
      "Transmission"
    ]=>array(2){
      [
        "Transmission"
      ]=>string(21)"5 Speed Manual Petrol"[
        "Transmission_UID"
      ]=>string(36)"707ad700-1318-4e57-88aa-6fb19e58f6d0"
    }[
      "UnitOfDistance"
    ]=>int(0)[
      "Vehicle_UID"
    ]=>string(36)"8be4d43d-e3a4-e611-80cf-0a94ef0355af"[
      "VehicleCode"
    ]=>string(40)"VAAD12JAM3HPIM "[
      "VehicleType"
    ]=>int(1)[
      "VideoCount"
    ]=>int(1)[
      "Videos"
    ]=>array(0){

    }[
      "VIN"
    ]=>string(17)"W0L0MAP08G6082024"
  }

2 个答案:

答案 0 :(得分:0)

JSON几乎肯定会返回载具数组,一旦解析后,就可以像其他任何可以迭代的数组一样,并且可以通过使用foreach完全忽略索引:

* {
  box-sizing: border-box; /** that's it ! **/
}

aside,
article,
section,
header,
footer,
nav {
  display: block;
}

div,
p {
  margin: 0;
  padding: 0;
}

html {
  background: #ccc;
}

.outer {
  /* TOTAL WIDTH: 700px */
  width: 600px;
  margin: 0 auto;
  background: #9CF;
  padding: 50px;
}

.box {
  background: #B7D19C;
  /* TOTAL WIDTH: 700px */
  width: 500px;
  padding: 98px;
  border: 2px black solid;
}

p {
  background: #FF9;
  height: 100%;
  /* here */
}

答案 1 :(得分:0)

最简单的示例: 函数array_map将自定义函数应用于$ response中的每个项目。

array_map(function ($car) {
   echo car['BasicPrice'];
   echo "<br>";
   echo $car['BodyStyle']['BodyStyle'];
   echo "<br>";
}, $response);