复杂的多维关联数组过程与foreach

时间:2012-08-20 17:49:50

标签: php arrays multidimensional-array foreach

我不得不再次问这个,对不起,但是我在尝试处理这个数组时遇到了问题。我尝试了几种不同的方法,但没有一种方法正确,这是数组:

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] => 
        )
      )
    )
  ) 
)

好的,我需要遍历数组并创建一个循环,因此对于每个ROOM实例,它将重复该过程。然后我需要从房间数组中提取数据并使用它来为每个房间实例填充MySQL中的行。

这是我到目前为止的代码,它打印房间数组中的名称和值。但是,它只获得一个房间阵列。 我可以做些什么来阅读所有房间?我也认为这对于每个房间来说太多了但是似乎无法遍历[''] [''] [' “] ... 或者只使用关联名称。

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

             }
      }

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

2 个答案:

答案 0 :(得分:3)

我认为你可以真的简化这句话。如果你知道这将永远是结构,那么你可以直接进入酒店,然后进入房间。

foreach($arr['search']['lr_rates']['hotel'] as $hotel) {
      // You can access all of the keys in the hotel array here

      foreach($hotel['hotel_rooms'] as $room) {
          // Do stuff with the room array
      }  
}

我建议您动态构建插入脚本并仅为写入调用数据库一次,或者如果您正在使用事务进行更新。随着房间数量的增加,您将通过一堆写入磁盘来减慢脚本速度。

答案 1 :(得分:2)

数据输出的格式非常糟糕且无法读取。我无法确定你想要做什么。

  1. 可能性:内部数组[hotel_rooms] => Array ()多次使用键room。由于数组键是唯一的,因此您将覆盖索引room处的数据。这就是为什么你只有一个房间。

  2. 可能性:rooms内有room - >使用递归函数迭代所有房间,如下所示:

    function handleRoom(array $room) {
    
        // do something with $room
    
        if (array_key_exists('room', $room)) {
    
            handleRoom($room['room']);
        }
    }
    
    $room = array(
        'some' => 'room',
        'data' => 'and another',
        'room' => array(
            'is' => 'inside',
            'of the' => 'main room',
        ),
    );
    
    handleRoom($room);
    
相关问题