dongyuan4790 2016-09-29 09:01
浏览 55
已采纳

PHP使用Key嵌套foreach

I have been trying to work this out for two days now and am hitting a brick wall. I have a skyscanner array that has flight itineraries where I have the flight

Leg - being Itineraries -> OutboundLegId - 

and also the legs which shows the flight number - being

Legs -> FlightNumbers -> FlightNumber.

What I am trying to achieve is to display the Itinerary and then join the Flight Number on to that. But no matter what I try I cannot get this to work. I have read all about Keys on here and tried loads of examples but am coming up with nothing. Can someone point me in the right direction please?

Example arrays below

[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条回答 默认 最新

  • dongtanxi5676756 2016-09-29 09:21
    关注

    Assuming this is one big array and its called $data you can nest a couple of foreach loops.

    I use foreach loops as I assume there are cases where this data structure get more complex than the one you show

    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'];
    
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)