消除 API 输出数据中的换行符

Eliminating line breaks in API output data

我是 API 集成和 PHP 的新手。我最近在我的应用程序中集成了一个 VIN 解码器。在输入框中输入车辆的 VIN,单击提交,有关该车辆的信息将被 return 编辑。

问题在于某些车辆 return 的数据比其他车辆多。奥迪 A4,VIN:WAUBFAFL6FA058452,所有字段的 returns 数据。但是,保时捷 911,VIN:WP0AB29954S696067,returns 数据仅用于部分字段。

输出结果如下。

奥迪A4:

VIN: WAUBFAFL6FA058452

Engine-

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

保时捷911:

VIN: WP0AB29954S696067

Engine-

Engine Displacement 1: 3.6 liters
Engine Displacement 2: 3600.0 cc's
Engine Displacement 3: 219.68547874103 ci's
Engine Size: 6 cylinders
Horsepower: 415 hp
Kilowatts: 309.4655 kw


Primary Fuel Type: Gasoline

我想做的是消除空数据字段导致的数据间隙。因为没有 "Engine Manufacturer and Engine Model," 的数据,所以输出数据中存在间隙。我怎样才能消除这个差距?因此,保时捷 911 所需的输出将如下所示:

期望的保时捷输出:

VIN: WP0AB29954S696067

Engine-

Engine Displacement 1: 3.6 liters
Engine Displacement 2: 3600.0 cc's
Engine Displacement 3: 219.68547874103 ci's
Engine Size: 6 cylinders
Horsepower: 415 hp
Kilowatts: 309.4655 kw
Primary Fuel Type: Gasoline

这是我的 php 代码:

<?php

$vin = $_POST["b12"];

if ($vin) {
$postdata = http_build_query([
        'format' => 'json',
        'data' => $vin
    ]
);
$opts = [
    'http' => [
        'method' => 'POST',
        'content' => $postdata
    ]
];

$apiURL = "https://vpic.nhtsa.dot.gov/api/vehicles/DecodeVINValuesBatch/";
$context = stream_context_create($opts);
$fp = fopen($apiURL, 'rb', false, $context);
$line_of_text = fgets($fp);
$json = json_decode($line_of_text, true);
fclose($fp);

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

  if ($k == "DisplacementCC"){
    $k  = "Engine Displacement 2";
  }
  if ($k == "DisplacementCI"){
    $k  = "Engine Displacement 3";
  }
  if ($k == "DisplacementL"){
    $k  = "Engine Displacement 1";
    $v = round($v,1);
  }
  if ($k == "EngineKW"){
    $k  = "Kilowatts";
  }
  if ($k == "EngineManufacturer"){
    $k  = "Engine Manufacturer";
  }
  if ($k == "EngineModel"){
    $k  = "Engine Model";
  }
  if ($k == "FuelTypePrimary"){
    $k  = "Primary Fuel Type";
  }
  if ($k == "FuelTypeSecondary"){
    $k  = "Secondary Fuel Type";
  }
  if ($k == "EngineHP"){
    $k  = "Horsepower";
  }
  if ($k == "EngineCylinders"){
    $k  = "Engine Size";
  }

  if (!empty($v)) {
    $data[$k] = ($k).": ".($v);
  }
}

echo $data['VIN'].'<br /><br/>';

echo "Engine-".'<br /><br />';
echo $data['Engine Displacement 1']. " liters". '<br />';
echo $data['Engine Displacement 2']. " cc's". '<br />';
echo $data['Engine Displacement 3']. " ci's". '<br />';
echo $data['Engine Size']. " cylinders". '<br />';
echo $data['Horsepower']." hp". '<br />';
echo $data['Kilowatts']." kw". '<br />';
echo $data['Engine Manufacturer']. '<br />';
echo $data['Engine Model']. '<br />';
echo $data['Primary Fuel Type']. '<br />';
echo $data['Secondary Fuel Type']. '<br /><br />';


  }

else {
echo 'No Vin Inputted';
  }




?>

最简单的方法就是创建一个小的实用函数,如果定义了每个度量,它可以选择性地打印它:

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

然后你会这样称呼它:

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/>';

print_if_not_empty 函数接受一个数组、该数组的一个键和一个可选的后缀。它检查以确保该键存在于数组中并且它不为空,如果是,它打印具有指定后缀的值。如果它不在数组中或者它是空的,它什么都不打印。

  1. 如果没有可显示的值,请不要为新的键名或单位值而烦恼。
  2. 通过将数据存储在 new/filtered/prepared 结果数组中,您可以更好地控制 <br /> 标记——这避免了在输出的最后一行之后写入中断标记。 (清洁工 dom)
  3. 我建议您使用包含 div 和样式在输出中创建空格而不是多个 <br /> 标记。
  4. 使用 elseif 语句,这样您就不会在找到 $k 匹配后不必要地检查后续条件。

代码:

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>";

输出:

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