PHP 带键的嵌套 foreach

PHP Nested foreach with Key

两天来我一直在努力解决这个问题,但遇到了困难。我有一个 skyscanner 数组,其中包含我的航班行程

Leg - being Itineraries -> OutboundLegId - 

还有显示航班号的腿 -

Legs -> FlightNumbers -> FlightNumber.

我想要实现的是显示行程,然后将航班号加入其中。但无论我尝试什么,我都无法让它发挥作用。我在这里阅读了所有关于 Keys 的内容,并尝试了很多示例,但一无所获。有人能给我指出正确的方向吗?

示例数组如下

[Itineraries] => Array
(
    [0] => Array
        (
            [OutboundLegId] => 13542-1610140610-29-0-13445-1610141240
            [InboundLegId] => 13445-1610211340-29-0-13542-1610211640
            [PricingOptions] => Array
                (
                    [0] => Array
                        (
                            [Agents] => Array
                                (
                                    [0] => 2174187
                                )

                            [QuoteAgeInMinutes] => 31
                            [Price] => 200.98
                            [DeeplinkUrl] => http://partners.api.skyscanner.net/apiservices/deeplink/v2?_cje=5JlLCgyPUKY0hT8T0Ybh6dL0Xf0htAiHTFX7RU79eeI3XvrsxvEqP1QUJAoHiHRd&url=http%3a%2f%2fwww.apideeplink.com%2ftransport_deeplink%2f4.0%2fUK%2fen-gb%2fGBP%2fcook%2f2%2f13542.13445.2016-10-14%2c13445.13542.2016-10-21%2fair%2fairli%2fflights%3fitinerary%3dflight%7c-32294%7c1152%7c13542%7c2016-10-14T06%3a10%7c13445%7c2016-10-14T12%3a40%2cflight%7c-32294%7c1153%7c13445%7c2016-10-21T13%3a40%7c13542%7c2016-10-21T16%3a40%26carriers%3d-32294%26passengers%3d1%2c0%2c0%26channel%3ddataapi%26cabin_class%3deconomy%26facilitated%3dfalse%26ticket_price%3d200.98%26is_npt%3dfalse%26is_multipart%3dfalse%26client_id%3dskyscanner_b2b%26request_id%3d3bc96bda-fd7c-403a-b841-2ccc3c26071d%26commercial_filters%3dfalse%26q_datetime_utc%3d2016-09-29T08%3a18%3a27
                        )

[Legs] => Array
(
    [0] => Array
        (
            [Id] => 13542-1610140610-29-0-13445-1610141240
            [SegmentIds] => Array
                (
                    [0] => 1
                )

            [OriginStation] => 13542
            [DestinationStation] => 13445
            [Departure] => 2016-10-14T06:10:00
            [Arrival] => 2016-10-14T12:40:00
            [Duration] => 270
            [JourneyMode] => Flight
            [Stops] => Array
                (
                )

            [Carriers] => Array
                (
                    [0] => 105
                )

            [OperatingCarriers] => Array
                (
                    [0] => 105
                )

            [Directionality] => Outbound
            [FlightNumbers] => Array
                (
                    [0] => Array
                        (
                            [FlightNumber] => 1152
                            [CarrierId] => 105
                        )

                )

        )

将其用作草稿。没有反馈就无法执行功能。 放置适当的数组而不是 {YOUR-ARRAY-WITH-LEGS} 和 {YOUR-ARRAY-WITH-ITINERARIES}

$sortedLegs = array_column('Id', {YOUR-ARRAY-WITH-LEGS});

$joinedArray = array_map(function($itinerary) use($sortedLegs){
    if(array_key_exists($itinerary['OutboundLegId'],$sortedLegs)) {
        $itinerary['legs'] = $sortedLegs[$itinerary['OutboundLegId']];
    }
  return $itinerary;
},{YOUR-ARRAY-WITH-ITINERARIES});

假设这是一个大数组,它被称为 $data 你可以嵌套几个 foreach 循环。

我使用 foreach 循环,因为我假设在某些情况下此数据结构比您显示的更复杂

foreach ( $data['Itineraries'] as $itin ) {

    foreach ( $data['Legs'] as $legs) {
        if ($legs['Id'] == $itin['OutboundLegId']) {

            // we matched the itinerary with a leg
            echo $legs['OutboundLegId'] . ' ' . $legs['FlightNumbers'][0]['FlightNumber'];

        }
    }
}