0

我有如下代码。我正在阅读一个 JSON URL 并用一些 if 条件回显一些项目。我需要以 JSON 格式重新回显所选项目。

<?php
$currentDate = date("Y/m/d");
$url         = "myURL";
$json        = file_get_contents($url);

$obj = json_decode($json);
foreach ($obj->flightStatuses as $flightstatus) {
    echo $flightstatus->carrierFsCode, ' ', $flightstatus->flightNumber, "\n";
    if ($flightstatus->carrierFsCode == 'G8') {
        echo "Goair\n";
    } elseif ($flightstatus->carrierFsCode == 'SG') {
        echo "Spicejet\n";
    } elseif ($flightstatus->carrierFsCode == '9W') {
        echo "Jetairways\n";
    } elseif ($flightstatus->carrierFsCode == 'S2') {
        echo "JetConnect\n";
    } elseif ($flightstatus->carrierFsCode == '6E') {
        echo "Indigo\n";
    } elseif ($flightstatus->carrierFsCode == 'AI') {
        echo "Air India\n";
    }
    ;
    if ($flightstatus->departureAirportFsCode == 'BOM') {
        echo "Mumbai\n";
    } elseif ($flightstatus->departureAirportFsCode == 'DEL') {
        echo "Delhi\n";
    } elseif ($flightstatus->departureAirportFsCode == 'SXR') {
        echo "Srinagar\n";
    } elseif ($flightstatus->departureAirportFsCode == 'IXJ') {
        echo "Jammu\n";
    }
    ;
    echo date('H:i:s', strtotime((string) $flightstatus->departureDate->dateLocal)), "\n";
    echo date('H:i:s', strtotime((string) $flightstatus->arrivalDate->dateLocal)), "\n";
    if ($flightstatus->status == 'A') {
        echo "Active \n";
    } elseif ($flightstatus->status == 'C') {
        echo "Cancelled\n";
    } elseif ($flightstatus->status == 'D') {
        echo "Diverted\n";
    } elseif ($flightstatus->status == 'L') {
        echo "Landed\n";
    } elseif ($flightstatus->status == 'S') {
        echo "Scheduled\n";
    } elseif ($flightstatus->status == 'U') {
        echo "Unknown\n";
    } elseif ($flightstatus->status == 'NO') {
        echo "Not Operational\n";
    } elseif ($flightstatus->status == 'DN') {
        echo "Data source needed";
    } else {
        echo "Redirected\n";
    }
    "\n";
    if ($flightstatus->arrivalAirportFsCode == 'BOM') {
        echo "Mumbai\n";
    } elseif ($flightstatus->arrivalAirportFsCode == 'DEL') {
        echo "Delhi\n";
    } elseif ($flightstatus->arrivalAirportFsCode == 'SXR') {
        echo "Srinagar\n";
    } elseif ($flightstatus->arrivalAirportFsCode == 'IXL') {
        echo "Leh\n";
    } elseif ($flightstatus->arrivalAirportFsCode == 'IXJ') {
        echo "Jammu\n";
    }
    ;
    echo $startT = gmdate("i:s", $flightstatus->flightDurations->scheduledBlockMinutes), "\n";
    echo $flightstatus->airportResources->departureTerminal, "\n\n\n\n";
}
?>

上面代码中的回显项导致我的值像

AI 821
Air India
Delhi
11:05:00
12:25:00
Scheduled
Jammu
01:20
3



SG 852
Spicejet
Srinagar
11:55:00
12:40:00
Scheduled
Jammu
00:45




G8 187
Goair
Delhi
12:30:00
13:45:00
Scheduled
Jammu
01:15
1

如何以 JSON 格式再次回显输出?

另外,如果我将变量分配给

$startTime= "date('H:i:s', strtotime((string) $flightstatus->departureDate->dateLocal)),";

它抛出了我的语法错误。

4

3 回答 3

2

正如上面@Barmar所建议的,您需要将数据存储到数组中,最后,回显数组的 json_encoded 版本。

  1. 启动临时数组:

    在这一行之间添加代码:

    $obj = json_decode($json);
    foreach ($obj->flightStatuses as $flightstatus) {
    

    编码:

    $tempResponseArrays = array();
    $obj = json_decode($json);
    foreach ($obj->flightStatuses as $flightstatus) {
        $tempResponse = array();
    

    笔记:

    1. $tempResponseArrays: 用于全局响应数组。
    2. $tempResponse: 用于单个响应数组(在循环内使用)。
  2. 对于每个回声,替换为如下内容:

    if ($flightstatus->carrierFsCode == 'G8') {
        echo "Goair\n";
    }
    

    变成:

    if ($flightstatus->carrierFsCode == 'G8') {
        $tempResponse['carrier'] = "Goair";
    }
    

    为其余的回显响应执行此操作。

  3. 在循环代码的最后一个将临时数组添加到全局临时数组:

    $tempResponseArrays[] = $tempResponse;
    
  4. 最后,循环完成后,回显 json 版本:

    echo json_encode($tempResponseArrays);
    
于 2013-05-07T05:50:37.370 回答
1

您可以将值转换为 json,您可以使用 json_encode 函数,

        string json_encode ( mixed $value [, int $options = 0 ] );

例如:

将数据保存在数组中,避免回显

  $data['place']="Mumbai\n";
  $data['flight']="Air India\n";

然后使用

 echo json_encode($data);

在这里您可以了解更多详情

于 2013-05-07T05:46:31.260 回答
0

在我看来,您已经在$json变量中以 json 格式开头的数据......

echo json_encode($json);

应该做的伎俩。

于 2013-05-07T05:43:40.037 回答