0

我在尝试处理此数组时遇到问题尝试了几种不同的方法,但都没有正确的方法,这是数组

Array ( 
  [search] => Array ( 
    [response] => Array ( 
      [errors] => 
      [number_of_hotels] => 1 of 1 
    ) 
    [lr_rates] => Array ( 
      [hotel] => Array  ( 
        [hotel_ref] => 3116 
        [hotel_currency] => [U] => USD 
        [hotel_rooms] => Array ( 
          [room] => Array ( 
            [ref] => 6382 
            [type] => 1 
            [type_description] => Standard 
            [sleeps] => 8 
            [rooms_available] => 
            [adults] => 8 
            [children] => 
            [breakfast] => false 
            [dinner] => false 
            [description] => 
            [alternate_description] => 
            [rack_rate] => 82.01 
            [date] => 19/08/201220/08/201221/08/2012
            [numeric_hotelcurrencyprice] => FullFullFull
            [formatted_date] => 19 August 201220 August 201221 August 2012 
            [price] => FullFullFull
            [hotelcurrencyprice] => FullFullFull 
            [numeric_price] => FullFullFull
            [requested_currency] => GBPGBPGBP 
            [numeric_hotelcurrencyprice] => FullFullFull
            [available_online] => false 
            [minimum_nights] => 1 
            [bed_type] => 
            [cancellation_policy] => 
            [cancellation_days] => 
            [cancellation_hours] => 
            [room_terms] => 
          )
          [room] => Array ( 
            [ref] => 6382 
            [type] => 1 
            [type_description] => Standard 
            [sleeps] => 8 
            [rooms_available] => 
            [adults] => 8 
            [children] => 
            [breakfast] => false 
            [dinner] => false 
            [description] => 
            [alternate_description] => 
            [rack_rate] => 82.01 
            [date] => 19/08/201220/08/201221/08/2012
            [numeric_hotelcurrencyprice] => FullFullFull
            [formatted_date] => 19 August 201220 August 201221 August 2012 
            [price] => FullFullFull
            [hotelcurrencyprice] => FullFullFull 
            [numeric_price] => FullFullFull
            [requested_currency] => GBPGBPGBP 
            [numeric_hotelcurrencyprice] => FullFullFull
            [available_online] => false 
            [minimum_nights] => 1 
            [bed_type] => 
            [cancellation_policy] => 
            [cancellation_days] => 
            [cancellation_hours] => 
            [room_terms] => 
          )
        ) 
        [cancellation_type] => First Night Stay Chargeable 
        [cancellation_policy] => 2 Days Prior to Arrival 

        [CityTax] => Array ( 
          [TypeName] => 
          [Value] => 
          [OptedIn] => 
          [IsCityTaxArea] => 
        )
      )
    )
  ) 
)

好的,我需要遍历数组并创建一个循环,因此对于每个房间实例,它将重复该过程然后我需要从房间数组中提取数据并使用它来填充 MySQL 中的行,这将是多个房间实例到目前为止,我有代码打印房间数组中的名称和值,但它只获取房间数组之一我可以做些什么来设置它来阅读它们,我也认为这对于每个人来说太多了,但不要'似乎无法遍历 ['']['']['']... 或仅使用关联名称。

foreach($arr['search'] as $lr_rates) {
        foreach($lr_rates['hotel'] as $hotel) {
                   foreach($hotel['room'] as  $field => $value){
                     print $field;print $value;
                          }

             }
      }

还值得一提的是,这些数组中的值总是在波动

4

2 回答 2

3

您不必在脚本中使用太多 foreach 循环,因为它看起来不太好。这是一个关联数组。您可以使用其键简单地访问关联数组。为它做一些谷歌。你可以找到很多关于这个的脚本。

于 2012-08-20T12:03:40.057 回答
2
foreach($arr as $search) {
    foreach($search as $lr_rates) {
        foreach($lr_rates as $hotel) {
                 foreach($hotel as $hotel_rooms) {
                      print_r($hotel_rooms['room'])
                  }
            }
     }
}

编辑:这些许多foreach循环只是为了了解如何到达room. 当然也可以直接得到结果。

print_r($arr['search']['lr_rates']['hotel']['hotel_rooms']['room']);

于 2012-08-20T09:32:08.197 回答