通过循环另一个数组创建新数组

发布于 2024-10-14 10:02:49 字数 13110 浏览 4 评论 0原文

我需要整理一个数组。并将所有类似信息收集在一起。

这将是我要提交的一个巨大订单,但是,它远远超出了我的技能范围,我已经尝试了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

天暗了我发光 2024-10-21 10:02:49

我想这样的 foreach 循环就足够了。 PHP 在更新时不会检查数组成员是否存在,因此它只会添加所需的键。

$HotelRooms = array();
$foreach($result as $r)
{
  $HotelName = $r->RoomInformationExtend->RoomDetails->Description;
  $HotelRooms[$HotelName][$r->RoomInformationExtend->RoomDetails->RoomId][] = $r->Details->EffectiveDate
}

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.

$HotelRooms = array();
$foreach($result as $r)
{
  $HotelName = $r->RoomInformationExtend->RoomDetails->Description;
  $HotelRooms[$HotelName][$r->RoomInformationExtend->RoomDetails->RoomId][] = $r->Details->EffectiveDate
}
枕头说它不想醒 2024-10-21 10:02:49

当您执行此操作时...

$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')
    )
)

这应该更容易使用,并且更接近您想要的东西。

When you do this...

$hotelinfo[count($hotelinfo)] = array();
$hotelinfo[count($hotelinfo)][$HotelName] = array();

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:

$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];

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:

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

After you've done this, you'll wind up with a structure in $hotelInfo that looks like this:

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

Which should be much easier to work with, and a lot closer to what you seem to be wanting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文