我有如下代码。我正在阅读一个 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)),";
它抛出了我的语法错误。