Changing the Layout of API Data Output

后端 未结 2 458
误落风尘
误落风尘 2021-01-22 05:48

I am new to API integration and PHP. I recently integrated a VIN decoder into my app. Enter a vehicle\'s VIN into the input box, select submit, and all information regarding tha

相关标签:
2条回答
  • 2021-01-22 06:26

    I recommend a "master" array to filter and prepare your categorically grouped data. See inline comments for more explanation:

    Code: (Demo)

    Your Master Array:

    $master = [
        "General" => [
            "Make" => "",
            "ModelYear" => "",
            "Model" => "",
            "BodyClass" => "",
            "Doors" => "",
            "Series" => "",
            "VehicleType" => ""
        ],
        "Safety" => [
            "AirBagLocCurtain" => "",
            "AirBagLocFront" => "",
            "AirBagLocSide" => "",
            "SeatBeltsAll" => ""
        ],
        "Engine" => [
            "DisplacementCC" => "",
            "DisplacementCI" => "",
            "DisplacementL" => "",
            "EngineCylinders" => "",
            "EngineHP" => "",
            "EngineKW" => "",
            "EngineManufacturer" => "",
            "EngineModel" => "",
            "TransmissionStyle" => "",
            "OtherEngineInfo" => "",
            "FuelTypePrimary" => "",
            "FuelTypeSecondary" => ""
        ],
        "Factory" => [
            "Manufacturer" => "",
            "ManufacturerId" => "",
            "PlantCity" => "",
            "PlantCountry" => ""
        ],
        "Other" => [
            "ErrorCode" => "",
            "TPMS" => ""
        ]
    ];
    

    Processing:

    foreach ($master as $category => &$items) {  // allow modification of $master data with &
        foreach ($items as $k => &$v) {          // allow modification of $master data with &
            if (isset($json['Results'][0][$k]) && strlen($json['Results'][0][$k])) {  // only bother to process/display desired keys and non-empty values
                $new = $json['Results'][0][$k];
                if ($k == "DisplacementCC") {
                    $v = "Engine Displacement 2: $new cc's";
                } elseif ($k == "DisplacementCI") {
                    $v = "Engine Displacement 3: $new ci's";
                } elseif ($k == "DisplacementL") {
                    $v = "Engine Displacement 1: " . round($new, 1) . " liters";
                } elseif ($k == "EngineKW") {
                    $v = "Kilowatts: $new kw";
                } elseif ($k == "EngineManufacturer") {
                    $v = "Engine Manufacturer: $new";
                } elseif ($k == "EngineModel") {
                    $v = "Engine Model: $new";
                } elseif ($k == "FuelTypePrimary") {
                    $v = "Primary Fuel Type: $new";
                } elseif ($k == "FuelTypeSecondary") {
                    $v = "Secondary Fuel Type: $new";
                } elseif ($k == "EngineHP") {
                    $v = "Horsepower: $new hp";
                } elseif ($k == "EngineCylinders") {
                    $v = "Engine Size: $new cylinders";
                } else {
                    $v = "$k: $new";
                }
            } else {
                unset($master[$category][$k]);  // remove unwanted element from master
            }
        }
    }
    
    unset($items, $v);  // just as a precaution to eliminate the referenced variables
    
    echo "<div id=\"VIN\">{$json['Results'][0]['VIN']}</div>\n\n";
    // now iterate the updated $master multi-dimensional array and only display the "good stuff"
    foreach ($master as $category => $items) {
        if (!empty($items)) {  // only display categories & rows of data when the category holds 1 or more values
            echo "<div class=\"group\">$category -</ br>";
            foreach ($items as $v) {
                echo "<div class=\"row\">$v</div>";
            }
            echo "</div>";
        }
    }
    

    Output:

    <div id="VIN">WAUBFAFL6FA058452</div>
    
    <div class="group">General -</ br>
        <div class="row">Make: AUDI</div>
        <div class="row">ModelYear: 2015</div>
        <div class="row">Model: A4</div>
        <div class="row">BodyClass: Sedan/Saloon</div>
        <div class="row">Doors: 4</div>
        <div class="row">Series: Premium quattro</div>
        <div class="row">VehicleType: PASSENGER CAR</div>
    </div>
    
    <div class="group">Safety -</ br>
        <div class="row">AirBagLocCurtain: All Rows</div>
        <div class="row">AirBagLocFront: 1st Row (Driver & Passenger)</div>
        <div class="row">AirBagLocSide: 1st Row (Driver & Passenger)</div>
        <div class="row">SeatBeltsAll: Manual</div>
    </div>
    
    <div class="group">Engine -</ br>
        <div class="row">Engine Displacement 2: 1984 cc's</div>
        <div class="row">Engine Displacement 3: 121.071108283 ci's</div>
        <div class="row">Engine Displacement 1: 2 liters</div>
        <div class="row">Engine Size: 4 cylinders</div>
        <div class="row">Horsepower: 220 hp</div>
        <div class="row">Kilowatts: 164.0540 kw</div>
        <div class="row">Engine Manufacturer: Audi</div>
        <div class="row">Engine Model: Flex Fuel Capable engine</div>
        <div class="row">TransmissionStyle: Automatic</div>
        <div class="row">OtherEngineInfo: Fuel: Gas (50-St); Federal / California Emission Standard: BIN 5 / ULEV II; Emissions Certification Test Group: FVGAV02.0AUB / FVGAJ02.0AUF E85</div>
        <div class="row">Primary Fuel Type: Gasoline</div>
        <div class="row">Secondary Fuel Type: Ethanol (E85)</div>
    </div>
    
    <div class="group">Factory -</ br>
        <div class="row">Manufacturer: AUDI</div>
        <div class="row">ManufacturerId: 1149</div>
        <div class="row">PlantCity: Ingolstadt</div>
        <div class="row">PlantCountry: Germany</div>
    </div>
    
    <div class="group">Other -</ br>
        <div class="row">ErrorCode: 0 - VIN decoded clean. Check Digit (9th position) is correct</div>
        <div class="row">TPMS: Indirect</div>
    </div>
    

    *note, you can use !empty() rather than isset() then strlen() ONLY if you KNOW that you will never have 0 as a valid value. empty() will mistake 0 as a "falsy"/"empty" value and silently call for the element's removal from the master array.

    0 讨论(0)
  • 2021-01-22 06:27

    You made a good start with

    foreach ($json['Results'][0] as $k => $v){
      if (!empty($v)) {
        $results .= ($k).": ".($v).'<br />';
      }
    }
    

    but before your start printing output try creating a your own array for example

    $data = array();
    foreach ($json['Results'][0] as $k => $v){
          if (!empty($v)) {
            $data[$k] = $v;
          }
        }
    

    Then you could take your time and echo the list you want. With this simple method, hopefully the fields you want in your list being excluded by the blank check are very low. Then you would have to do something about that.

    echo 'Make: '. $data['Make'] . '<br>';
    echo 'ModelYear: '. $data['ModelYear']. '<br>';
    echo 'Model: '. $data['Model']. '<br>';
    echo 'BodyClass: '. $data['BodyClass']. '<br>';
    echo 'Doors: '. $data['Doors']. '<br>';
    echo 'Series: '. $data['Series']. '<br>';
    echo 'VehicleType: '. $data['VehicleType']. '<br>';
    

    You could also build your desired data array manually with only keys and blank values then allow your foreach to populate it. Then you could use another forech to print your data array as it will be in your desired order.

    Just a suggestion. Hope it helps you get a little further in your process.

    0 讨论(0)
提交回复
热议问题