如何合并键值匹配的数组元素

时间:2016-06-18 13:16:42

标签: php arrays

我在该数组中有一个数组我想合并元素,但条件是如果Key“HotelCode”与另一个元素的“HotelCode”匹配则合并否则将其保留。

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

        )
)



例如:

MyArray 中,我想要在阵列上插入多个阵列,但条件是我必须检查 HotelCode 键值是否匹配,如果是匹配然后保持它否则离开它。

喜欢数组

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



在上面的数组中,你可以看到我做了什么。

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

但正如您所见

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.

2 个答案:

答案 0 :(得分:2)

不完全确定您的问题是什么,但如果您只想构建一个房间价格按酒店代码分组的数组,那么您可以这样做:

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

此代码构建一个由HotelCode索引的新数组,并将所有房间价格添加到其中。最后一行删除了HotelCode索引键。

答案 1 :(得分:1)

你应该试试这个

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