Skip to content Skip to sidebar Skip to footer

Eliminating Line Breaks In API Output Data

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, click submit, and information regarding that vehicle

Solution 1:

The easiest thing to do would be to just create a little utility function that optionally prints each metric if it is defined:

<?php
function print_if_not_empty(array &$arr, $key, $suffix = '') {
    if (!empty($arr[$key])) {
        echo $arr[$key] . ' ' . $suffix . '<br />';
    }
}

And then you would call it like this:

print_if_not_empty($data, 'Engine Displacement 1', "liters");
print_if_not_empty($data, 'Engine Displacement 2', "cc's");
print_if_not_empty($data, 'Engine Displacement 3', "ci's");
print_if_not_empty($data, 'Engine Size', "cylinders");
print_if_not_empty($data, 'Horsepower', "hp");
print_if_not_empty($data, 'Kilowatts', "kw");
print_if_not_empty($data, 'Engine Manufacturer');
print_if_not_empty($data, 'Engine Model');
print_if_not_empty($data, 'Primary Fuel Type');
print_if_not_empty($data, 'Secondary Fuel Type');
echo '<br/>';

The print_if_not_empty function takes an array, a key into that array, and an optional suffix. It checks to make sure that the key exists in the array and that it is not empty, and if so it prints the value with the specified suffix. If it’s not in the array or it is and it is empty, it prints nothing.


Solution 2:

  1. Don't bother with the new key names or unit values if there is no value to display.
  2. By storing the data in a new/filtered/prepared results array, you have better control of the <br /> tags -- this avoids writing a break tag after the last line of output. (cleaner dom)
  3. I suggest that you use containing divs and styling to create spaces in your output rather than multiple <br /> tags.
  4. Use elseif statements so that you aren't needlessly checking subsequent conditions after you've found a $k match.

Code:

foreach ($json['Results'][0] as $k => $v){
    if (strlen($v)) {  // only bother to process this element if it contains a value with a positive length
        if ($k == "DisplacementCC") {
            $results[]  = "Engine Displacement 2: $v cc's";
        } elseif ($k == "DisplacementCI") {
            $results[] = "Engine Displacement 3: $v ci's";
        } elseif ($k == "DisplacementL") {
            $results[] = "Engine Displacement 1: " . round($v, 1) . " liters";
        } elseif ($k == "EngineKW") {
            $results[] = "Kilowatts: $v kw";
        } elseif ($k == "EngineManufacturer") {
            $results[] = "Engine Manufacturer: $v";
        } elseif ($k == "EngineModel") {
            $results[] = "Engine Model: $v";
        } elseif ($k == "FuelTypePrimary") {
            $results[] = "Primary Fuel Type: $v";
        } elseif ($k == "FuelTypeSecondary") {
            $results[] = "Secondary Fuel Type: $v";
        } elseif ($k == "EngineHP") {
            $results[] = "Horsepower: $v hp";
        } elseif ($k == "EngineCylinders") {
            $results[] = "Engine Size: $v cylinders";
        }
    }
}

echo "<div id=\"VIN\">{$json['Results'][0]['VIN']}</div>";
echo "<div id=\"EngineDetails\">";
    echo "Engine-<br /><br />";
    echo implode("<br />", $results);
echo "</div>";

Output:

<div id="VIN">WAUBFAFL6FA058452</div>
<div id="EngineDetails">
Engine-<br /><br />

Engine Displacement 2: 1984 cc's<br />
Engine Displacement 3: 121.071108283 ci's<br />
Engine Displacement 1: 2 liters<br />
Engine Size: 4 cylinders<br />
Horsepower: 220 hp<br />
Kilowatts: 164.0540 kw<br />
Engine Manufacturer: Audi<br />
Engine Model: Flex Fuel Capable engine<br />
Primary Fuel Type: Gasoline<br />
Secondary Fuel Type: Ethanol (E85)
</div>

Post a Comment for "Eliminating Line Breaks In API Output Data"