多级数组 - 如何循环并显示第3级

时间:2015-04-14 09:26:40

标签: php arrays

我目前正致力于与Expedia API进行通信。 Expedia API的响应是一个4级深度的JSON数组。我可以通过以下方式访问这些值:例如。 $数据[' HotelListResponse'] [' HotelList'] [' HotelSummary'] [' 0'] [' rateCurrencyCode&#39 ;] - 但是我有多个记录可供访问,我无法循环显示记录以显示所有酒店记录的列表。我已经尝试了各种不同的循环结果的方法,但我完全停留在这里,并没有找到任何关于如何在多级数组中循环更深层次的例子。

这是我对API的回应:

Array
(
    [HotelListResponse] => Array
        (
            [customerSessionId] => 0ABAAAD6-0372-E191-4CB2-719DEDD9325E
            [numberOfRoomsRequested] => 0
            [moreResultsAvailable] => 
            [HotelList] => Array
                (
                    [@size] => 232
                    [@activePropertyCount] => 232
                    [HotelSummary] => Array
                        (
                            [0] => Array
                                (
                                    [@order] => 0
                                    [hotelId] => 382498
                                    [name] => Georgetown Inn
                                    [address1] => 6100 Corson Avenue
                                    [city] => Seattle
                                    [stateProvinceCode] => WA
                                    [postalCode] => 98108
                                    [countryCode] => US
                                    [airportCode] => SEA
                                    [propertyCategory] => 1
                                    [hotelRating] => 2.5
                                    [confidenceRating] => 52
                                    [amenityMask] => 134234122
                                    [locationDescription] => Near Boeing Plant 2
                                    [shortDescription] => <p><b>Property Location</b> <br />With a stay at Georgetown Inn in Seattle, you'll be close to Boeing Plant 2 and Safeco Field. This hotel is within close proximity of Jefferson Park Golf Course and
                                    [highRate] => 80.25
                                    [lowRate] => 79.25
                                    [rateCurrencyCode] => USD
                                    [latitude] => 47.5478
                                    [longitude] => -122.32137
                                    [proximityDistance] => 11.007573
                                    [proximityUnit] => MI
                                    [hotelInDestination] => 1
                                    [thumbNailUrl] => /hotels/5000000/4450000/4444400/4444333/4444333_86_t.jpg
                                    [deepLink] => http://www.travelnow.com/templates/484006/hotels/382498/overview?lang=en&currency=USD&standardCheckin=null/null/null&standardCheckout=null/null/null
                                )

                            [1] => Array
                                (
                                    [@order] => 1
                                    [hotelId] => 125727
                                    [name] => Motif Seattle
                                    [address1] => 1415 5th Ave
                                    [city] => Seattle
                                    [stateProvinceCode] => WA
                                    [postalCode] => 98101
                                    [countryCode] => US
                                    [airportCode] => SEA
                                    [propertyCategory] => 1
                                    [hotelRating] => 4
                                    [confidenceRating] => 52
                                    [amenityMask] => 7798851
                                    [locationDescription] => Near Pike Place Market
                                    [shortDescription] => <p><b>Property Location</b> <br />With a stay at Motif Seattle, you'll be centrally located in Seattle, steps from 5th Avenue Theater and minutes from Pike Place Market. This 4-star hotel is within
                                    [highRate] => 339
                                    [lowRate] => 239
                                    [rateCurrencyCode] => USD
                                    [latitude] => 47.60985
                                    [longitude] => -122.33475
                                    [proximityDistance] => 11.168453
                                    [proximityUnit] => MI
                                    [hotelInDestination] => 1
                                    [thumbNailUrl] => /hotels/1000000/60000/51000/50947/50947_264_t.jpg
                                    [deepLink] => http://www.travelnow.com/templates/484006/hotels/125727/overview?lang=en&currency=USD&standardCheckin=null/null/null&standardCheckout=null/null/null
                                )

                            [2] => Array
                                (
                                    [@order] => 2
                                    [hotelId] => 164122
                                    [name] => The Westin Seattle
                                    [address1] => 1900 5th Ave
                                    [city] => Seattle
                                    [stateProvinceCode] => WA
                                    [postalCode] => 98101
                                    [countryCode] => US
                                    [airportCode] => SEA
                                    [propertyCategory] => 1
                                    [hotelRating] => 4
                                    [confidenceRating] => 5
                                    [amenityMask] => 7819587
                                    [locationDescription] => Near Pike Place Market
                                    [shortDescription] => <p><b>Property Location</b> <br />With a stay at The Westin Seattle, you'll be centrally located in Seattle, steps from Westlake Center and minutes from Pacific Place. This 4-star hotel is close to
                                    [highRate] => 239
                                    [lowRate] => 189
                                    [rateCurrencyCode] => USD
                                    [latitude] => 47.61309
                                    [longitude] => -122.33788
                                    [proximityDistance] => 11.335604
                                    [proximityUnit] => MI
                                    [hotelInDestination] => 1
                                    [thumbNailUrl] => /hotels/1000000/20000/16700/16673/16673_301_t.jpg
                                    [deepLink] => http://www.travelnow.com/templates/484006/hotels/164122/overview?lang=en&currency=USD&standardCheckin=null/null/null&standardCheckout=null/null/null
                                )

                            [3] => Array
                                (
                                    [@order] => 3
                                    [hotelId] => 164116
                                    [name] => Sheraton Seattle Hotel
                                    [address1] => 1400 6th Ave
                                    [city] => Seattle
                                    [stateProvinceCode] => WA
                                    [postalCode] => 98101
                                    [countryCode] => US
                                    [airportCode] => SEA
                                    [propertyCategory] => 1
                                    [hotelRating] => 4
                                    [confidenceRating] => 30
                                    [amenityMask] => 278595
                                    [locationDescription] => Near Pike Place Market
                                    [shortDescription] => <p><b>Property Location</b> <br />With a stay at Sheraton Seattle Hotel, you'll be centrally located in Seattle, steps from 5th Avenue Theater and Washington State Convention Center. This 4-star
                                    [highRate] => 450
                                    [lowRate] => 295
                                    [rateCurrencyCode] => USD
                                    [latitude] => 47.61017
                                    [longitude] => -122.33357
                                    [proximityDistance] => 11.115907
                                    [proximityUnit] => MI
                                    [hotelInDestination] => 1
                                    [thumbNailUrl] => /hotels/1000000/30000/25300/25290/25290_88_t.jpg
                                    [deepLink] => http://www.travelnow.com/templates/484006/hotels/164116/overview?lang=en&currency=USD&standardCheckin=null/null/null&standardCheckout=null/null/null
                                ) ......

我正在尝试循环并显示数组中[0] [1] [2] [3]级别的值。任何人都可以帮我提供有关如何编写foreach循环的信息吗?

2 个答案:

答案 0 :(得分:0)

foreach ($data['HotelListResponse']['HotelList']['HotelSummary'] as $i=>$a) {
    echo $a['rateCurrencyCode'];
}

答案 1 :(得分:0)

你需要实现递归函数:

function doLoop($foo) {
 if (is_array($foo)) {
   foreach ($foo as $bar) {
    doLoop($bar);
   }
 } else {
  /* do your staff with $foo */
 }
}

doLoop($foo);