简体   繁体   English

从另一个数组替换数组中的值

[英]Replacing values in array from another array

I have this array structure: 我有这个数组结构:

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
)

And this array structure: 和这个数组结构:

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
        )

)

Basically what I want to do is take the first array and find its matching IATA/FS code in the second array and replace it with the ICAO code. 基本上,我要执行的操作是获取第一个数组,并在第二个数组中找到其匹配的IATA/FS代码,然后将其替换为ICAO代码。 So for example, with the first array, I want to replace the VX with VRD . 因此,例如,对于第一个阵列,我想将VX替换为VRD I want to be able to apply the same concept to other airline/routes, too...not just VX . 我也希望能够将相同的概念应用于其他航空公司/航线……而不仅仅是VX

If it helps, I'm getting this information from a JSON return: http://pastebin.com/2w0kQQ26 如果有帮助,我将从JSON返回中获取此信息: http : //pastebin.com/2w0kQQ26

I looked into array_replace() , but because my PHP skills are almost nothing, I didn't know how to continue. 我研究了array_replace() ,但是由于我的PHP技能几乎没有,所以我不知道如何继续。

If someone can point me to the right direction, I'd appreciate it. 如果有人可以指出正确的方向,我将不胜感激。

With array1 being your first array and array2 being the second array you described. 其中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]];
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM