1

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

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;
                          }

             }
      }

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

4

2 回答 2

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
      }  
}

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

于 2012-08-20T17:59:27.793 回答
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);
    
于 2012-08-20T18:00:48.967 回答