我有一个如下代码。我正在阅读一个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";
}
?>
上面代码中的echo项会产生像
这样的值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)),";
它会引发语法错误。
答案 0 :(得分:2)
正如上面@Barmar所建议的那样,你需要将数据存储到数组中,最后,回显json_encoded版本的数组。
启动临时数组:
在此行之间添加代码:
$obj = json_decode($json);
foreach ($obj->flightStatuses as $flightstatus) {
代码:
$tempResponseArrays = array();
$obj = json_decode($json);
foreach ($obj->flightStatuses as $flightstatus) {
$tempResponse = array();
注意:
$tempResponseArrays
:for global response array。- 醇>
$tempResponse
:用于个别响应数组(在循环内使用)。
对于每个回声,替换为这样的东西:
if ($flightstatus->carrierFsCode == 'G8') {
echo "Goair\n";
}
变成:
if ($flightstatus->carrierFsCode == 'G8') {
$tempResponse['carrier'] = "Goair";
}
为你的其他回应做出这样做。
在最后一个循环代码中将临时个体数组添加到全局临时数组:
$tempResponseArrays[] = $tempResponse;
最后,循环完成后,回显json版本:
echo json_encode($tempResponseArrays);
答案 1 :(得分:1)
您可以将值转换为json,您可以使用json_encode函数,
string json_encode ( mixed $value [, int $options = 0 ] );
<强>例如强>
将数据保存到数组中,Insted of echoing
$data['place']="Mumbai\n";
$data['flight']="Air India\n";
然后使用
echo json_encode($data);
Here您可以获得更多详情
答案 2 :(得分:0)
在我看来,您已经拥有json格式的数据,以$json
变量开头......
echo json_encode($json);
应该这样做。