dongnao1908 2018-08-20 22:08
浏览 111
已采纳

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

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 is returned.

The issue is some vehicles return more data than others. An Audi A4, VIN: WAUBFAFL6FA058452, returns data for all of the fields. However, a Porsche 911, VIN: WP0AB29954S696067, returns data for only some of the fields.

Here are how the outputs look.

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

Porsche 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

What I want to do is eliminate the gap in data caused by empty data fields. Because there is no data for "Engine Manufacturer and Engine Model," there is a gap in the output data. How can I eliminate this gap? The desired output for the Porsche 911 would therefore look like this:

Desired Porsche Output:

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

Here is my php code:

<?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';
  }




?>
  • 写回答

2条回答 默认 最新

  • download1323 2018-08-20 22:15
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 idea自动补全键位冲突
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 定制ai直播实时换脸软件
  • ¥100 栈回溯相关,模块加载后KiExceptionDispatch无法正常回溯了
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错
  • ¥15 java python或者任何一种编程语言复刻一个网页