PHP使用Key嵌套foreach

时间:2016-09-29 09:01:47

标签: php foreach key skyscanner

我一直试图解决这个问题两天,而且正在撞砖墙。我有一个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
                        )

                )

        )

2 个答案:

答案 0 :(得分: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});

答案 1 :(得分:0)

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

        }
    }
}