0

我已经尝试解决这个问题两天了,并且正在碰壁。我有一个 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
                        )

                )

        )
4

2 回答 2

1

假设这是一个大数组并且它被调用$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'];

        }
    }
}
于 2016-09-29T09:21:27.387 回答
0

将其用作草稿。没有反馈就无法执行功能。放置正确的数组而不是 {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});
于 2016-09-29T09:19:46.140 回答