2016-06-18 64 views
2

我在該數組中有一個數組我想要合併元素,但條件是如果鍵「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. 
+0

Array_merge是你 – CiroRa

+0

我需要用array_merge(),因爲我已經嘗試 – User97798

回答

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>'; 
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並增加所有p室水稻。最後一行刪除HotelCode索引鍵。

+0

你給有關數組FUNTASTIC想法,非常感謝你來的foreach。 – User97798

相關問題