我有一个如下代码。我正在阅读一个JSON URL并使用一些if条件回显一些项目。我需要以JSON格式回显所选项目。
<?php
// Array of trains to list
//Arrival train list
$trainNumbers = array(
9021,11077
);
$json = file_get_contents('myURL.json');
$trainData = json_decode($json, true);
foreach ($trainData[0] as $train) {
$trainNumber = $train[0][0];
if (in_array($trainNumber, $trainNumbers)) {
$fields = array(
'train_no',
'train_name',
'dep_date',
'dep_station',
'dep_log',
'dep_lat',
'arr_station',
'delay_time',
'new_lat',
'new_long',
'new_station',
'new_station_name',
'time_delay',
'station_left'
);
foreach ($train[0] as $i => $dataField) {
echo $fields[$i] . " - {$dataField}\n";
$trains[$trainNumber][$fields[$i]] = $datafield;
}
echo "\n";
}
}
?>
上面的代码显示数据
train_no - 09021
train_name - MUMBAI BANDRA T - JAMMU TAWI Exp (SPL)
dep_date - 2013-05-06
dep_station - BRSQ
dep_log - 28.613196
dep_lat - 77.14046
arr_station - BRAR SQUARE
delay_time - 150
new_lat - 28.659977
new_long - 77.156425
new_station - UMB
new_station_name - AMBALA CANT JN
time_delay - 48
station_left - 67
train_no - 11077
train_name - PUNE - JAMMU TAWI Jhelum Express
dep_date - 2013-05-06
dep_station - HET
dep_log - 26.611628
dep_lat - 77.943449
arr_station - HETAMPUR
delay_time - 56
new_lat - 26.697312
new_long - 77.905769
new_station - DHO
new_station_name - DHAULPUR
time_delay - 44
station_left - 93
如何以JSON格式再次回显输出?
按照以下建议进行编辑
$data[$fields[$i]]= " - {$dataField}\n";
echo json_encode($data);
我收到此错误
这是JSON输出
实际上在这个脚本中我们有100列车的列表,所以我从那里打了一些列车。
答案 0 :(得分:1)
答案 1 :(得分:1)
目前,您的问题是您将每个已映射的“列车”作为单独项目输出。为了使输出表示有效的JSON,至少应将单独的元素收集到一个数组中。以下是程序的略微修改版本:
<?php
// Fields to be translated
$fields = array(
'train_no',
'train_name',
'dep_date',
'dep_station',
'dep_log',
'dep_lat',
'arr_station',
'delay_time',
'new_lat',
'new_long',
'new_station',
'new_station_name',
'time_delay',
'station_left'
);
// Array of trains to list
//Arrival train list
$trainNumbers = array(
9021,11077
);
$json = file_get_contents('myurl.json');
$trainData = json_decode($json, true);
foreach ($trainData[0] as $train) {
$trainNumber = $train[0][0];
if (in_array($trainNumber, $trainNumbers)) {
foreach ($train[0] as $i => $dataField) {
$data[$fields[$i]]= $dataField;
}
$translated[] = $data;
}
}
echo json_encode($translated);
?>
哪些输出(使用jsonlint格式化后):
[
{
"train_no": "09021",
"train_name": "MUMBAI BANDRA T - JAMMU TAWI Exp (SPL)",
"dep_date": "2013-05-06",
"dep_station": "HUK",
"dep_log": "28.801247",
"dep_lat": "77.102394",
"arr_station": "HOLAMBI KALAN",
"delay_time": "172",
"new_lat": "28.846516",
"new_long": "77.085357",
"new_station": "UMB",
"new_station_name": "AMBALA CANT JN",
"time_delay": 70,
"station_left": 64
},
{
"train_no": "11077",
"train_name": "PUNE - JAMMU TAWI Jhelum Express",
"dep_date": "2013-05-06",
"dep_station": "BHA",
"dep_log": "27.06879445",
"dep_lat": "77.96653748",
"arr_station": "BHANDAI",
"delay_time": "59",
"new_lat": "27.157722",
"new_long": "77.989883",
"new_station": "AGC",
"new_station_name": "AGRA CANTT",
"time_delay": 45,
"station_left": 92
}
]
您可以选择使用不同类型的JSON输出(例如,根据名称或其他任何内容键入的每根火车的根对象)。只需扩展上面显示的相同原则,以生成有效的JSON。