0

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?

JS1986
  • 1,920
  • 4
  • 29
  • 50

2 Answers2

2

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
}
Vlad
  • 165
  • 5
  • Will take a little bit more code, due to the fact that not all of the EffectiveDates are in the same place in each record. See my answer for more details. – Amber Jan 23 '11 at 05:53
1

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.

Amber
  • 507,862
  • 82
  • 626
  • 550
  • most certainly isnt :( will make that change accordingly – JS1986 Jan 23 '11 at 05:33
  • It's ma'am, but because I'm feeling nice, take a look at the added section above. – Amber Jan 23 '11 at 05:51
  • much appreciate and pardon me. thanks for taking the extreme effort to help me out. – JS1986 Jan 23 '11 at 05:54
  • My bad, didn't see the different data format because iPad clips overflow of code divs. It's annoying at times. – Vlad Jan 23 '11 at 06:00
  • Hi Amber, i tried the code but i guess im missing something, i returns the hotelname but there is nothing inside the array of each hotel – JS1986 Jan 23 '11 at 07:23