Changing the Layout of API Data Output

后端 未结 2 459
误落风尘
误落风尘 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 "
    {$json['Results'][0]['VIN']}
    \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 "
    $category -"; foreach ($items as $v) { echo "
    $v
    "; } echo "
    "; } }

    Output:

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

    *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.

提交回复
热议问题