User97798
User97798

Reputation: 644

How to merge element of an array where a key value match

I Have an array in that array i want to merge element but the condition is if Key "HotelCode" match with another element's "HotelCode" Then merge otherwise left it.

MyArray

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [HotelCode] => ASSOC01
                    [Price] => Array
                        (
                            [RoomPrice] => 200
                        )
                )

            [1] => Array
                (
                    [HotelCode] => ASSOC02
                    [Price] => Array
                        (
                            [RoomPrice] => 210
                        )
                )

            [2] => Array
                (
                    [HotelCode] => ASSOC03
                    [Price] => Array
                        (
                            [RoomPrice] => 220
                        )
                )
            [3] => Array
                (
                    [HotelCode] => ASSOC04
                    [Price] => Array
                        (
                            [RoomPrice] => 230
                        )
                )
        )

    [1] => Array
        (

            [0] => Array
                (
                    [HotelCode] => ASSOC02
                    [Price] => Array
                        (
                            [RoomPrice] => 310
                        )
                )

            [1] => Array
                (
                    [HotelCode] => ASSOC01
                    [Price] => Array
                        (
                            [RoomPrice] => 300
                        )
                )



            [2] => Array
                (
                    [HotelCode] => ASSOC03
                    [Price] => Array
                        (
                            [RoomPrice] => 320
                        )
                )

            [3] => Array
                (
                    [HotelCode] => ASSOC04
                    [Price] => Array
                        (
                            [RoomPrice] => 330
                        )
                )
        )

    [2] => Array
        (

            [0] => Array
                (
                    [HotelCode] => ASSOC03
                    [Price] => Array
                        (
                            [RoomPrice] => 420
                        )
                )  

            [1] => Array
                (
                    [HotelCode] => ASSOC01
                    [Price] => Array
                        (
                            [RoomPrice] => 400
                        )
                )

            [3] => Array
                (
                    [HotelCode] => ASSOC02
                    [Price] => Array
                        (
                            [RoomPrice] => 410
                        )
                )

        )
)



For Example:

In MyArray there is multiple array that array i want to into on array, but the condition is i have to check HotelCode key value is matching or not if it is matching then keep it otherwise left it.

Like Below Array:

Array
(

    [0] => Array
        (
            [HotelCode] => ASSOC01
            [Room] => Array
                (
                    [0] => Array
                        (
                            [RoomPrice] => 200
                        )
                    [1] => Array
                        (
                            [RoomPrice] => 300
                        )
                    [2] => Array
                        (
                            [RoomPrice] => 400
                        )
                )
        )

    [1] => Array
        (
            [HotelCode] => ASSOC02
            [Room] => Array
                (
                    [0] => Array
                        (
                            [RoomPrice] => 210
                        )
                    [1] => Array
                        (
                            [RoomPrice] => 310
                        )
                    [2] => Array
                        (
                            [RoomPrice] => 410
                        )
                )
        )

    [2] => Array
        (
            [HotelCode] => ASSOC03
            [Room] => Array
                (
                    [0] => Array
                        (
                            [RoomPrice] => 220
                        )
                    [1] => Array
                        (
                            [RoomPrice] => 320
                        )
                    [2] => Array
                        (
                            [RoomPrice] => 420
                        )
                )
        )
)



In above array you can see what i did.

MyArray[0][0][HotelCode], MyArray[1][1][HotelCode] and MyArray[2][1][HotelCode] values are same then i kept in an array

But as you can see

MyArray[0][3][HotelCode] and MyArray[1][3][HotelCode] are same but that "HotelCode" value is not available in MyArray[2] than i just left it.

Upvotes: 2

Views: 73

Answers (2)

Ajay Kumar
Ajay Kumar

Reputation: 1352

You should try this

$array = array(...); // Your MyArray data
$Temp_array = [];
// Merging array here 
foreach($array as $value) {
    foreach($value as $key1 => $value1) {
        $Temp_array[$value1['HotelCode']][$key1] = $value1;
        $Temp_array[$value1['HotelCode']]['Room'][] = $value1['Price'];
        unset($Temp_array[$value1['HotelCode']]['Price']);
    }
}

$Temp_array2 = array_values($Temp_array);

// Removing Incompete array Here 
$Final_Result_Array = [];

foreach ($Temp_array2 as $key2 => $value2) {
    if (count($array) == count($value1['Room'])) {
        $Final_Result_Array[] = $value2;
    }
}

echo '<pre>';
print_r($Final_Result_Array);
echo '</pre>';

Upvotes: 1

Pieter
Pieter

Reputation: 1774

Not entirely sure what your question is, but if you just want to build an array in which the room prices are grouped by hotel code, then you can do this:

$data = ...; // The original MyArray data
$mergedData = [];

foreach($data as $hotelPrices) {
    foreach($hotelPrices as $roomPrice) {
        $hotelCode = $roomPrice['HotelCode'];
        $mergedData[$hotelCode]['HotelCode'] = $hotelCode;
        $mergedData[$hotelCode]['Room'][] = $roomPrice['Price'];
    }
}

// If the array must be numerically indexed
$numericIndexedData = array_values($mergedData);

This code builds a new array indexed by HotelCode and adds all the room prices to it. The last line removes the HotelCode index keys.

Upvotes: 2

Related Questions