通过循环另一个数组创建新数组
我需要整理一个数组。并将所有类似信息收集在一起。
这将是我要提交的一个巨大订单,但是,它远远超出了我的技能范围,我已经尝试了 3 天来解决它。
该消息来自网络服务,我完全无法控制输出/响应。
我需要能够输入所有信息 但以下(我知道这很抱歉)是 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 个带有 ID 的房间。这两个 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++;
}
}
}
我是否朝着正确的方向前进?
I need to tidy up an array. And gather all similar information together.
This is going to be a huge order that I'm bringing to the table, however, it's well beyond my skill set and I've been trying to fix it for 3 days.
The message comes from a webservice, which i have absolutely no control over the output/response.
I need to be able to put all the info
But the following (i know its big sorry) is the SOAP object reply: (this has been reduced as well) 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
Each hotel has 2 rooms with ids. Each of these 2 id's are seperated by dates, but are in the different parts of the array :(
[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
I want to be able to put them into an array as such:
[Hotel A]
HotelA RoomId = 01
Date 12-08-2011
Date 13-08-2011
HotelA RoomId = 02
Date 12-08-2011
Date 13-08-2011
etc
Please help. I've been pulling my hair out :(
Here is an example of what ive tried:
$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++;
}
}
}
was i even going in thr correct direction with this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这样的 foreach 循环就足够了。 PHP 在更新时不会检查数组成员是否存在,因此它只会添加所需的键。
I suppose such a foreach loop would be sufficient. PHP doesn't check the existence of array members on updating, so it would just add the needed keys.
当您执行此操作时...
您实际上是向
$hotelinfo
添加了两个 元素。例如,如果其中已有 4 个元素,则第一行将设置索引 4,这是一个新索引(现有索引为 0、1、2 和 3),从而添加一个元素。现在第一行已完成,$hotelinfo 中的元素数量为 5,因此第二行设置索引 5 - 但索引 5 对应于下一个空槽,因此第二个元素是添加(对于设置索引 0-5 的最终结果,其中 4 和 5 都是新的)。
从您的代码来看,我认为这不是您的意图。
加倍努力(或两英里)...
我认为您可能首先要做的是将数据转换为更可用的格式 - 首先按酒店分组,然后按房间 ID 分组。那么,让我们这样做:
好的,现在我们有一个可以为其填充信息的
$thisRoom
对象。如果我们正在浏览的列表中的另一项已经为特定的酒店+房间组合创建了一个对象,我们只需获取已创建的对象,而不是创建一个新对象。现在,我们将把当前数据项的所有相关信息添加到我们的房间对象中:
完成此操作后,您将在
$hotelInfo
中得到一个如下所示的结构:这应该更容易使用,并且更接近您想要的东西。
When you do this...
You're actually adding two elements to
$hotelinfo
. For instance, if there were 4 elements in it already, the first line would set index 4, which is a new index (the existing ones are 0, 1, 2, and 3), thus adding an element.Now that the first line is done, the number of elements in
$hotelinfo
is 5, and thus the second line sets index 5 - but index 5 corresponds to the next empty slot, and thus a second element is added (for an end result of indices 0-5 being set, with 4 and 5 both new).Judging from your code, I don't think this is what you intended.
Going the extra mile (or two)...
I think what you might want to do first is get the data into a more usable format - grouped first by hotel, and then by room ID. So, let's do that:
Okay, so now we have a
$thisRoom
object that we can fill in info for. If a different item in the list we're going through already created an object for a particular hotel+room combination, we just get that already-created one instead of making a new one.Now, we'll add any relevant information is the current item of data to our room object:
After you've done this, you'll wind up with a structure in
$hotelInfo
that looks like this:Which should be much easier to work with, and a lot closer to what you seem to be wanting.