Php 基于foreach的复多维关联数组处理
很抱歉,我不得不再次询问这个问题,但我在尝试处理此数组时遇到问题。我尝试了几种不同的方法,但没有一种是正确的,以下是阵列:Php 基于foreach的复多维关联数组处理,php,arrays,multidimensional-array,foreach,Php,Arrays,Multidimensional Array,Foreach,很抱歉,我不得不再次询问这个问题,但我在尝试处理此数组时遇到问题。我尝试了几种不同的方法,但没有一种是正确的,以下是阵列: Array ( [search] => Array ( [response] => Array ( [errors] => [number_of_hotels] => 1 of 1 ) [lr_rates] => Array ( [hotel] => 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] =>
)
)
)
)
)
好的,我需要遍历数组并创建一个循环,所以对于房间的每个实例,它都会重复这个过程。然后我需要从room数组中提取数据,并使用它在MySQL中为room的每个实例填充行
这是我到目前为止所拥有的代码,它打印房间数组中的名称和值。但是,它只获得一个房间阵列。我能做些什么来阅读所有的房间?我还认为这对每个人来说都太多了,但似乎无法向下遍历[]。。。
或者只使用关联名称
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
}
}
我建议您动态构建insert脚本并调用数据库一次,或者如果您正在更新,则使用事务。随着文件室数量的增加,您将通过大量的磁盘写入操作来降低脚本的速度。我认为您可以将此引用简化一点。如果你知道这将永远是一个结构,那么你可以跳到酒店,然后进入房间
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
}
}
我建议您动态构建insert脚本并调用数据库一次,或者如果您正在更新,则使用事务。随着文件室数量的增加,您将通过大量磁盘写入来降低脚本的速度。数据输出的格式非常糟糕,无法读取。我真的不知道你想做什么 可能性:内部阵列[hotel_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);
数据输出的格式非常糟糕,无法读取。我真的不知道你想做什么 可能性:内部阵列[hotel_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);
问题是什么?请使用标点符号,你的文章不可读。好的,那么当你有问题时让我们知道我编辑了这篇文章以提高可读性,并将问题加粗。我只需要有人接受它。问题是什么?请使用标点符号,你的文章不可读。好的,那么当你有问题时让我们知道我编辑了这篇文章以提高可读性,并将问题加粗。我只是需要有人接受它。