我有一个相当大的JSON文件,其中包含完整的机场列表及其各自的信息(总共46000行)。
我希望能够检查JSON文件是否包含ICAO,然后在变量中返回机场名称。
理想情况下这样(仅作为示例)
if ($jsonFile contains $icao) {
$airportName = airport_name of $icao;
} else {
echo 'Sorry, that ICAO could not be found';
}
[
{
"complete_location": "Anaa, Tuamotus, French Polynesia",
"iata_code": "AAA",
"city": "Anaa",
"icao_code": "NTGA",
"country": " French Polynesia",
"airport_name": "Anaa Airport"
},
{
"complete_location": "Arrabury, Queensland, Australia",
"iata_code": "AAB",
"city": "Arrabury",
"icao_code": "YARY",
"country": " Australia",
"airport_name": "Arrabury Airport"
},
{
"complete_location": "El Arish, Egypt",
"iata_code": "AAC",
"city": "El Arish",
"icao_code": "HEAR",
"country": " Egypt",
"airport_name": "El Arish International Airport"
},
如果我进入国际民航组织YARY
我会收到机场名称Arrabury Airport
到目前为止,我已设法在网页中打印所有json数据:
<?php
// The JSON file location
$JSONdatas = file_get_contents('icao/datas/city_airport_codes.json');
$ARRAYdatas = json_decode($JSONdatas);
?>
<div>
<div>
<table>
<thead>
<tr>
<th width="200">IATA Codes</th>
<th width="200">ICAO Codes</th>
<th width="200">Airport Names</th>
<th>Locations</th>
<th width="150">Cities</th>
<th width="150">Countries</th>
</tr>
</thead>
<tbody>
<?php
$rows = '';
foreach($ARRAYdatas as $key => $airport)
{
$rows .= '
<tr>
<td>'.$airport->iata_code.'</td>
<td>'.$airport->icao_code.'</td>
<td>'.$airport->airport_name.'</td>
<td>'.$airport->complete_location.'</td>
<td>'.$airport->city.'</td>
<td>'.$airport->country.'</td>
</tr>
';
}
echo $rows;
?>
</tbody>
</table>
</div>
</div>
但我不确定从哪里开始!
感谢。
答案 0 :(得分:3)
这是我的解决方案:
$condition = 'YARY';
$airports = array_filter($ARRAYdatas, function($item) use(&$condition) {
return isset($item->icao_code) && $item->icao_code == $condition;
});
$airport = array_pop($airports);
$airportName = $airport ? $airport->airport_name : '';
可能有一个更优雅或更快的解决方案,但我认为通过这种方式实现目标非常容易。
答案 1 :(得分:1)
如果您正在寻找一些性能提升,那么以本机PHP格式缓存json_decode()
输出并包含该文件可能是一个很好的调用。假设您在每个页面加载时包含.json文件。 (使用var_export()
将数据转储为PHP可读格式。)
答案 2 :(得分:0)
如果您熟悉回调,也可以使用:
function isICAO($Icao = NULL, $_json = array())
{
array_walk($_json, function($item, $key) use($Icao){
if($item->icao_code == $Icao) { echo $item->airport_name; }
});
}
// Print Airport name if icao_code is found. Else nothing.
echo isICAO("NTGA", $_json);
回调之前的老路
<?php
function isICAO($Icao = NULL, $_json = array())
{
foreach ($_json as $key => $item)
{
if($item->icao_code == $Icao)
{
return $item->airport_name;
}
}
return false;
}
if($Airport = isICAO("NTGA", $_json))
{
echo $Airport;
}
else
{
echo "Sorry, that ICAO could not be found";
}
?>
Javascript代码:
var json = [
{
"complete_location": "Anaa, Tuamotus, French Polynesia",
"iata_code": "AAA",
"city": "Anaa",
"icao_code": "NTGA",
"country": " French Polynesia",
"airport_name": "Anaa Airport"
},
{
"complete_location": "Arrabury, Queensland, Australia",
"iata_code": "AAB",
"city": "Arrabury",
"icao_code": "YARY",
"country": " Australia",
"airport_name": "Arrabury Airport"
},
{
"complete_location": "El Arish, Egypt",
"iata_code": "AAC",
"city": "El Arish",
"icao_code": "HEAR",
"country": " Egypt",
"airport_name": "El Arish International Airport"
}
];
function isICAO(Icao)
{
var result = [];
json.map(function(value, index) {
if(value.icao_code == Icao) { result.push(value); }
});
return result;
}
console.log(isICAO('HEAR'));
</script>