我需要整理一个阵列。并将所有类似的信息收集在一起。
这将是一个巨大的订单,我带到桌面,然而,它远远超出我的技能组合,我一直试图修复它3天。
该消息来自Web服务,我完全无法控制输出/响应。
我需要能够提供所有信息 但是以下(我知道很抱歉)是SOAP对象回复:(这也已经减少了)eek。
[0] => RoomInformation Object
(
[Details] => DetailsType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-12
[AvailabilityStatus] => AvailableForSale
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[RoomId] => 1860
[RoomNo] => THAH01
[Description] => Hotel Alpha
)
)
)
[1] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-12
[AvailabilityStatus] => AvailableForSale
)
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[RoomId] => 1861
[RoomNo] => THAH02
[Description] => Hotel Alpha
)
)
)
[2] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-12
[AvailabilityStatus] => AvailableForSale
)
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[Amenities] => stdClass Object
(
)
[Configurations] => stdClass Object
(
)
[RoomId] => 1226
[RoomNo] => DENM01
[Description] => Hotel Beta
)
)
)
[3] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-12
[AvailabilityStatus] => AvailableForSale
)
[MoreRatesExistInd] =>
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[Amenities] => stdClass Object
(
)
[Configurations] => stdClass Object
(
)
[RoomId] => 1227
[RoomNo] => DENM02
[Description] => Hotel Beta
)
)
)
[4] => RoomInformation Object
(
[Details] => DetailsType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-13
[AvailabilityStatus] => AvailableForSale
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[RoomId] => 1860
[RoomNo] => THAH01
[Description] => Hotel Alpha
)
)
)
[5] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-13
[AvailabilityStatus] => AvailableForSale
)
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[RoomId] => 1861
[RoomNo] => THAH02
[Description] => Hotel Alpha
)
)
)
[2] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-13
[AvailabilityStatus] => AvailableForSale
)
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[Amenities] => stdClass Object
(
)
[Configurations] => stdClass Object
(
)
[RoomId] => 1226
[RoomNo] => DENM01
[Description] => Hotel Beta
)
)
)
[3] => RoomInformation Object
(
[Details] => DetailsType Object
(
[RoomRate] => RoomRateType Object
(
[Total] => TotalType Object
(
[AmountAfterTax] => 1.00
)
[EffectiveDate] => 2011-08-13
[AvailabilityStatus] => AvailableForSale
)
[MoreRatesExistInd] =>
)
[RoomInformationExtend] => TPA_ExtensionsType Object
(
[RoomDetails] => RoomDetails Object
(
[Amenities] => stdClass Object
(
)
[Configurations] => stdClass Object
(
)
[RoomId] => 1227
[RoomNo] => DENM02
[Description] => Hotel Beta
)
)
)
TL; DR
每家酒店都有2间带有ids的房间。这两个id中的每一个都按日期分隔,但是在数组的不同部分:(
[Hotel A]
HotelA RoomId = 01
Date = 12-08-2011
[Hotel A]
HotelA RoomId = 02
Date = 12-08-2011
[Hotel B]
HotelB RoomId = 01
Date = 12-08-2011
[Hotel B]
HotelB RoomId = 02
Date = 12-08-2011
[Hotel A]
HotelA RoomId = 01
Date = 13-08-2011
[Hotel A]
HotelA RoomId = 02
Date = 13-08-2011
[Hotel B]
HotelB RoomId = 01
Date = 13-08-2011
[Hotel B]
HotelB RoomId = 02
Date = 13-08-2011
我希望能够将它们放入数组中:
[Hotel A]
HotelA RoomId = 01
Date 12-08-2011
Date 13-08-2011
HotelA RoomId = 02
Date 12-08-2011
Date 13-08-2011
等
请帮忙。我一直在拔头发:(
这是我尝试过的一个例子:
$hotelinfo = array();
$k=0;
for($i = 0; $i < count($result->RoomInformation); $i++)
{
$flag = false;
$HotelObj = $result->RoomInformation[$i];
$HotelName = $result->RoomInformation[$i]->RoomInformationExtend->RoomDetails->Description;
$HotelId = $result->RoomStays->RoomStay[$i]->RoomInformation->RoomDetails->RoomId;
//$hotelinfo[$i][$HotelName] = array();
$hotelinfo[count($hotelinfo)] = array();
$hotelinfo[count($hotelinfo)][$HotelName] = array();
for($c = 0; $c < count($result->RoomInformation); $c++)
{
$thishotelObj = $result->RRoomInformation[$c];
$thisHotelName = $thishotelObj->RoomInformationExtend->RoomDetails->Description;
$thisHotelId = $thishotelObj->TPA_Extensions->RoomDetails->RoomId;
if($thisHotelName == $HotelName & $thisHotelId == $HotelId){
$hotelinfo[$i][$HotelName][$c] = $HotelName;
$hotelinfo[$i][$HotelName][$c] = $thisHotelName;
$i++;
}
}
}
我是否正朝这个方向前进?
答案 0 :(得分:2)
我认为这样的foreach循环就足够了。 PHP不会在更新时检查数组成员是否存在,因此它只会添加所需的键。
$HotelRooms = array();
$foreach($result as $r)
{
$HotelName = $r->RoomInformationExtend->RoomDetails->Description;
$HotelRooms[$HotelName][$r->RoomInformationExtend->RoomDetails->RoomId][] = $r->Details->EffectiveDate
}
答案 1 :(得分:1)
当你这样做时......
$hotelinfo[count($hotelinfo)] = array();
$hotelinfo[count($hotelinfo)][$HotelName] = array();
您实际上是将两个元素添加到$hotelinfo
。例如,如果其中已有4个元素,则第一行将设置索引4,这是一个新索引(现有的索引为0,1,2和3),因此添加了一个元素。
现在第一行已完成,$hotelinfo
中的元素数量为5,因此第二行设置索引5 - 但索引5对应于下一个空槽,因此添加了第二个元素(设置索引0-5的最终结果,4和5都是新的)。
根据您的代码判断,我认为这不是您想要的。
我认为您首先要做的是将数据转换为更有用的格式 - 首先按酒店分组,然后按房间ID分组。所以,让我们这样做:
$hotelInfo = array();
foreach($result->RoomInformation as $roomInfo) {
$hotelName = $roomInfo->RoomInformationExtend->RoomDetails->Description;
if(!isset($hotelInfo[$hotelName])) {
$hotelInfo[$hotelName] = array();
}
$thisHotel = $hotelInfo[$hotelName];
$roomId = $roomInfo->RoomInformationExtend->RoomDetails->RoomId;
if(!isset($thisHotel[$roomId])) {
$thisHotel[$roomId] = array();
}
$thisRoom = $thisHotel[$roomId];
好的,现在我们有一个$thisRoom
对象,我们可以填写信息。如果列表中的其他项目已经为特定酒店+房间组合创建了一个对象,我们只需获取已经创建的对象而不是创建一个新对象。
现在,我们将向我们的房间对象添加任何相关信息,即当前数据项:
if(isset($roomInfo->Details)) {
if(isset($roomInfo->Details->EffectiveDate)) {
// "$foo[] = bar" appends 'bar' to the array '$foo'
$thisRoom[] = $roomInfo->Details->EffectiveDate;
}
if(isset($roomInfo->Details->RoomRate)) {
if(isset($roomInfo->Details->RoomRate->EffectiveDate)) {
$thisRoom[] = $roomInfo->Details->RoomRate->EffectiveDate;
}
}
}
}
完成此操作后,您将看到$hotelInfo
中的结构,如下所示:
array(
['Hotel Alpha'] => array(
[1860] => array('2011-08-12', '2011-08-13'),
[1861] => array('2011-08-12', '2011-08-13')
),
['Hotel Beta'] => array(
[1226] => array('2011-08-12', '2011-08-13'),
[1227] => array('2011-08-12', '2011-08-13')
)
)
哪个应该更容易使用,并且更接近你似乎想要的东西。