简体   繁体   中英

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. So for example, with the first array, I want to replace the VX with VRD . I want to be able to apply the same concept to other airline/routes, too...not just VX .

If it helps, I'm getting this information from a JSON return: http://pastebin.com/2w0kQQ26

I looked into array_replace() , but because my PHP skills are almost nothing, I didn't know how to continue.

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.

$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]];
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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