foreach处理复杂的多维联想数组


complex multidimentional associative array process with 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;
                          }
             }
      }

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

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

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. 可能性:内部数组CCD_ 1多次使用密钥CCD_。由于数组键是唯一的,因此覆盖索引room处的数据。这就是为什么你只有一个房间。

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

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