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;博士

每家酒店都有 2 间带身份证的房间。这 2 个 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

ETC

请帮忙。我一直在拔头发:(

这是我尝试过的示例:

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

我什至朝着正确的方向前进吗?

4

2 回答 2

2

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

$HotelRooms = array();
$foreach($result as $r)
{
  $HotelName = $r->RoomInformationExtend->RoomDetails->Description;
  $HotelRooms[$HotelName][$r->RoomInformationExtend->RoomDetails->RoomId][] = $r->Details->EffectiveDate
}
于 2011-01-23T05:35:20.293 回答
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')
    )
)

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

于 2011-01-23T05:26:41.163 回答