2014-06-21 99 views
0

我得到這個數組作爲mysql查詢的結果。我想從數組中刪除重複的hotel_id hotel_name,但保留其他數據。如何合併一個數組中的某些相同的鍵

array(6) { 
    [0] => array(17) { 
     ["id"] => string(1) "1" ["hotel_id"] => string(1) "1" ["name"] => string(31) "Vilamendhoo Island Resort & Spa" ["city"] => string(11) "Vilamendhoo" ["country"] => string(8) "Maldives" ["image"] => string(6) "01.jpg" ["address"] => string(0) "" ["wifi"] => string(1) "1" ["atoll"] => string(14) "South Ari Atol" ["type"] => string(6) "Resort" ["stars"] => string(1) "4" ["room_id"] => string(1) "1" ["room_type"] => string(29) "Garden view room - full board" ["description"] => string(47) "Limited Time Offer. Rate includes 20% discount!" ["availability"] => string(12) "only 4 rooms" ["rates"] => string(2) "25" ["breakfast"] => string(1) "1" 
    } [1] => array(17) { 
     ["id"] => string(1) "2" ["hotel_id"] => string(1) "1" ["name"] => string(31) "Vilamendhoo Island Resort & Spa" ["city"] => string(11) "Vilamendhoo" ["country"] => string(8) "Maldives" ["image"] => string(6) "01.jpg" ["address"] => string(0) "" ["wifi"] => string(1) "1" ["atoll"] => string(14) "South Ari Atol" ["type"] => string(6) "Resort" ["stars"] => string(1) "4" ["room_id"] => string(1) "2" ["room_type"] => string(24) "Beach villa - full board" ["description"] => string(47) "Limited Time Offer. Rate includes 20% discount!" ["availability"] => string(9) "Available" ["rates"] => string(2) "50" ["breakfast"] => string(1) "1" 
    } [2] => array(17) { 
     ["id"] => NULL ["hotel_id"] => NULL ["name"] => string(28) "Kuredu Island Resort and Spa" ["city"] => string(9) "Kuredhdhu" ["country"] => string(8) "Maldives" ["image"] => string(14) "12137_Main.jpg" ["address"] => string(0) "" ["wifi"] => NULL ["atoll"] => string(15) "Lhaviyani Atoll" ["type"] => string(6) "Resort" ["stars"] => string(1) "4" ["room_id"] => NULL ["room_type"] => NULL ["description"] => NULL ["availability"] => NULL ["rates"] => NULL ["breakfast"] => NULL 
    } [3] => array(17) { 
     ["id"] => NULL ["hotel_id"] => NULL ["name"] => string(39) "Lily Beach Resort & Spa - All Inclusive" ["city"] => string(11) "Huvahendhoo" ["country"] => string(8) "Maldives" ["image"] => string(14) "41480_Main.jpg" ["address"] => string(0) "" ["wifi"] => NULL ["atoll"] => string(15) "South Ari Atoll" ["type"] => string(6) "Resort" ["stars"] => string(1) "5" ["room_id"] => NULL ["room_type"] => NULL ["description"] => NULL ["availability"] => NULL ["rates"] => NULL ["breakfast"] => NULL 
    } [4] => array(17) { 
     ["id"] => NULL ["hotel_id"] => NULL ["name"] => string(26) "Vakarufalhi Island Resort " ["city"] => string(11) "Vakarufalhi" ["country"] => string(8) "Maldives" ["image"] => string(14) "41483_Main.jpg" ["address"] => string(62) "ADh. Vakarufalhi, South Ari Atoll, Maldives Islands, Maldives " ["wifi"] => NULL ["atoll"] => string(15) "South Ari Atoll" ["type"] => string(6) "Resort" ["stars"] => string(1) "4" ["room_id"] => NULL ["room_type"] => NULL ["description"] => NULL ["availability"] => NULL ["rates"] => NULL ["breakfast"] => NULL 
    } [5] => array(17) { 
     ["id"] => NULL ["hotel_id"] => NULL ["name"] => string(29) "Banyan Tree Vabbinfaru Resort" ["city"] => string(10) "Vabbinfaru" ["country"] => string(8) "Maldives" ["image"] => string(14) "41485_Main.jpg" ["address"] => string(0) "" ["wifi"] => NULL ["atoll"] => string(15) "South Ari Atoll" ["type"] => string(6) "Resort" ["stars"] => string(1) "5" ["room_id"] => NULL ["room_type"] => NULL ["description"] => NULL ["availability"] => NULL ["rates"] => NULL ["breakfast"] => NULL 
    } 
} 
+1

可能重複://計算器.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) –

回答

1

它容易得多,如果你在這一個使用索引name(酒店名),並將它們分配到一個新的數組。上面的第一個答案不能過濾相同的值,因爲它們不同(請注意第一個和第二個嵌套數組的索引id,它們不會匹配)。考慮下面的例子:

$original_values = array(array('id' => '1', 'hotel_id' => '1', 'name' => 'Vilamendhoo Island Resort & Spa', 'city' => 'Vilamendhoo', 'country' => 'Maldives', 'image' => '01.jpg', 'address' => '', 'wifi' => '1', 'atoll' => 'South Ari Atoll', 'type' => 'Resort', 'stars' => '4', 'room_id' => '1', 'room_type' => 'Garden view room - full board', 'description' => 'Limited Time Offer. Rate includes 20% discount!', 'availability' => 'only 4 rooms', 'rates' => '25', 'breakfast' => '1',), array('id' => '2', 'hotel_id' => '1', 'name' => 'Vilamendhoo Island Resort & Spa', 'city' => 'Vilamendhoo', 'country' => 'Maldives', 'image' => '01.jpg', 'address' => '', 'wifi' => '1', 'atoll' => 'South Ari Atoll', 'type' => 'Resort', 'stars' => '4', 'room_id' => '1', 'room_type' => 'Garden view room - full board', 'description' => 'Limited Time Offer. Rate includes 20% discount!', 'availability' => 'only 4 rooms', 'rates' => '25', 'breakfast' => '1',), array('id' => NULL, 'hotel_id' => NULL, 'name' => 'Kuredu Island Resort and Spa', 'city' => 'Kuredhdhu', 'country' => 'Maldives', 'image' => '12137_Main.jpg', 'address' => '', 'wifi' => NULL, 'atoll' => 'Lhaviyani Atoll', 'type' => 'Resort', 'stars' => '4', 'room_id' => NULL, 'room_type' => NULL, 'description' => NULL, 'availability' => NULL, 'rates' => NULL, 'breakfast' => NULL,), array('id' => NULL, 'hotel_id' => NULL, 'name' => 'Lily Beach Resort & Spa - All Inclusive', 'city' => 'Huvahendhoo', 'country' => 'Maldives', 'image' => '41480_Main.jpg', 'address' => '', 'wifi' => NULL, 'atoll' => 'South Ari Atoll', 'type' => 'Resort', 'stars' => '5', 'room_id' => NULL, 'room_type' => NULL, 'description' => NULL, 'availability' => NULL, 'rates' => NULL, 'breakfast' => NULL,), array('id' => NULL, 'hotel_id' => NULL, 'name' => 'Vakarufalhi Island Resort ', 'city' => 'Vakarufalhi', 'country' => 'Maldives', 'image' => '41483_Main.jpg', 'address' => 'ADh. Vakarufalhi, South Ari Atoll, Maldives Islands, Maldives ', 'wifi' => NULL, 'atoll' => 'South Ari Atoll', 'type' => 'Resort', 'stars' => '4', 'room_id' => NULL, 'room_type' => NULL, 'description' => NULL, 'availability' => NULL, 'rates' => NULL, 'breakfast' => NULL,), array('id' => NULL, 'hotel_id' => NULL, 'name' => 'Banyan Tree Vabbinfaru Resort', 'city' => 'Vabbinfaru', 'country' => 'Maldives', 'image' => '41485_Main.jpg', 'address' => '', 'wifi' => NULL, 'atoll' => 'South Ari Atoll', 'type' => 'Resort', 'stars' => '5', 'room_id' => NULL, 'room_type' => NULL, 'description' => NULL, 'availability' => NULL, 'rates' => NULL, 'breakfast' => NULL,),); 
$new_values = array(); 
foreach($original_values as $key => $value) { 
    $new_values[$value['name']][] = $value; 
} 

// simple reindex 
$new_values = array_values($new_values); 

echo '<pre>'; 
print_r($new_values); 

Sample Output

編輯:簡單輸出

foreach($new_values as $key => $values) { 
    foreach($values as $element) { 
     foreach($element as $index => $value) { 
      echo $index . ' => ' . $value . '<br/>'; 
     } 
    } 
} 
[如何從在PHP多維數組中刪除重複的值](HTTP的
+0

Dear Kevinabelita。 我試過你的代碼,它是完全刪除第二個(重複)項目。但我想保留其他數據。 簡單地說,如果一個酒店有不同類型的房間,那麼我想要停止酒店名稱重複,並顯示所有相關酒店名稱下的房間類型。 有沒有辦法做到這一點? 謝謝 – Yasitha

+1

@Yasitha哦好吧,你沒有提到它,但請等待我做一些修改。 – user1978142

+0

@Yasitha如果我理解正確嗎?你真的不打算刪除重複,如果有重複共享相同的酒店,相應地分組,他們是否正確?檢查我所做的修訂 – user1978142

2

我發現這個片段在這裏stackoverflow,這是非常有用的。

$input = array_map('unserialize', array_unique(array_map('serialize', $input))); 

以下是原始帖子的鏈接和簡要說明。
https://stackoverflow.com/a/946300/1121121

相關問題