我有这个数组结构:
stdClass Object
(
[carrierFsCode] => VX
[flightNumber] => 925
[departureAirportFsCode] => LAX
[arrivalAirportFsCode] => SFO
[stops] => 0
[departureTerminal] => 3
[arrivalTerminal] => 2
[departureTime] => 2014-04-28T07:00:00.000
[arrivalTime] => 2014-04-28T08:20:00.000
[flightEquipmentIataCode] => 32S
[isCodeshare] =>
[isWetlease] =>
[serviceType] => J
[serviceClasses] => Array
(
[0] => F
[1] => J
[2] => Y
)
[trafficRestrictions] => Array
(
)
[codeshares] => Array
(
[0] => stdClass Object
(
[carrierFsCode] => SQ
[flightNumber] => 1407
[serviceType] => J
[serviceClasses] => Array
(
[0] => R
[1] => F
[2] => J
[3] => Y
)
[trafficRestrictions] => Array
(
[0] => Q
)
[referenceCode] => 10594616
)
)
[referenceCode] => 979-1740743--
)
stdClass Object
(
[carrierFsCode] => SQ
[flightNumber] => 1407
[departureAirportFsCode] => LAX
[arrivalAirportFsCode] => SFO
[stops] => 0
[departureTerminal] => 3
[arrivalTerminal] => 2
[departureTime] => 2014-04-28T07:00:00.000
[arrivalTime] => 2014-04-28T08:20:00.000
[flightEquipmentIataCode] => 32S
[isCodeshare] => 1
[isWetlease] =>
[serviceType] => J
[serviceClasses] => Array
(
[0] => R
[1] => F
[2] => J
[3] => Y
)
[trafficRestrictions] => Array
(
[0] => Q
)
[operator] => stdClass Object
(
[carrierFsCode] => VX
[flightNumber] => 925
[serviceType] => J
[serviceClasses] => Array
(
[0] => F
[1] => J
[2] => Y
)
[trafficRestrictions] => Array
(
)
)
[codeshares] => Array
(
)
[referenceCode] => 979-1740743--10594616
)
这个数组结构:
Array
(
[0] => stdClass Object
(
[fs] => SQ
[iata] => SQ
[icao] => SIA
[name] => Singapore Airlines
[active] => 1
)
[1] => stdClass Object
(
[fs] => VX
[iata] => VX
[icao] => VRD
[name] => Virgin America
[active] => 1
)
)
基本上我想做的是获取第一个数组并在第二个数组中找到匹配的IATA/FS
代码,并将其替换为ICAO
代码。例如,对于第一个数组,我想用VX
替换VRD
。我希望能够将相同的概念应用于其他航空公司/航线......不仅仅是VX
。
如果有帮助,我将从JSON返回获取此信息:http://pastebin.com/2w0kQQ26
我调查了array_replace()
,但由于我的PHP技能几乎没有,我不知道如何继续。
如果有人能指出我正确的方向,我会很感激。
答案 0 :(得分:1)
array1
是您的第一个数组,array2
是您描述的第二个数组。
$comp_arr = array()
foreach ($array2 as $arr) {
$comp_arr[$arr[fs]] = $arr[icao];
}
foreach($array1 as $key => $arr){
if(array_key_exist($arr[carrierFsCode], $comp_arr){
$array1[$key][carrierFsCode] = $comp_arr[$arr[carrierFsCode]];
}
}