用foreach复杂的多维关联数组过程 [英] complex multidimentional associative array process with foreach

查看:138
本文介绍了用foreach复杂的多维关联数组过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不得不再次询问这一个,很抱歉,但我有一个问题,试图处理这个数组。我曾尝试几种不同的方式,但没有正确的地方,这里的数组:

I've had to ask this one again, sorry, but I'm having a problem trying to process this array. I have tried several different ways but none where right, here's the array:

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中的空间每个实例。

Ok, I need to traverse the array and create a loop, so for every instance of ROOM it will repeat the process. Then i need to extract the data from room array and use it to populate rows in MySQL for each instance of room.

这是code我迄今为止打印出房间数组中的名称和值。然而,它仅获取室阵列之一。 我能做些什么来阅读所有的客房吗我还想到,这是太多了,每个但似乎没有能够穿越下来[''] [''] [' '] ...
或者仅仅使用联想的名字。

This is the code I have so far which prints the names and values in the room array. However, it only gets one of the room arrays. What can I do to read all of the rooms? I am also thinking this is too many for-each but don't seem to be able to traverse down ['']['']['']... or by just using the associative name.

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

             }
      }

这也可能是值得一提的这些阵列的值总是起伏不定。

It might also be worth mentioning the values in these arrays are always fluctuating.

推荐答案

我觉得你真的可以简化这句话一点。如果你知道,这将永远是结构,那么,您可以直接到酒店,然后跳进房间。

I think you can really simplify this quote a bit. If you know that this will always be the structure then you can jump right down into the hotels and then into the rooms.

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

我会建议无论是构建插入脚本在运行并调用数据库只是一次写入,或者如果你正在使用事务更新即可。由于房间的数量变大,你会减缓你的脚本来与一群写入到磁盘中。

I would recommend either building your insert script on the fly and calling the database just once for the write, or if you are updating then using a transaction. As the number of rooms gets larger you will slow your script down with a bunch of writes to disk.

这篇关于用foreach复杂的多维关联数组过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆