0

I getting this array as a result for mysql query. I want to remove the duplicate hotel_id hotel_name from the array but keeping other data.

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
    }
}
Yasitha
  • 901
  • 2
  • 17
  • 42
  • 1
    possible duplicate of [How to remove duplicate values from a multi-dimensional array in PHP](http://stackoverflow.com/questions/307674/how-to-remove-duplicate-values-from-a-multi-dimensional-array-in-php) – Mark Miller Jun 21 '14 at 06:40

2 Answers2

2

I've found this snippet here on stackoverflow and it is really useful.

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

Here is the link to the original post with brief explanation.
https://stackoverflow.com/a/946300/1121121

Community
  • 1
  • 1
hutchbat
  • 806
  • 6
  • 14
1

Its much easier if you use the index name (hotel name) on this one and assign them to a new array. The first above answer wont be able to filter the same value because they are different (observe the index id of the first and second nested array, they wont match). Consider this example:

$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

Edit: Simple output

foreach($new_values as $key => $values) {
    foreach($values as $element) {
        foreach($element as $index => $value) {
            echo $index . ' => ' . $value . '<br/>';  
        }
    }
}
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Dear Kevinabelita. I tried your code it is compleatly remove the second (duplicate) Item. but I want to keep its other data. simply if one hotel is having different types of rooms then I want to stop hotel name repeating and display all the rooms type under relevent hotel name. is there a way to do this? thank you – Yasitha Jun 21 '14 at 11:27
  • 1
    @Yasitha oh okay, you did not mention it earlier though, wait il make some revision. – user1978142 Jun 21 '14 at 11:29
  • @Yasitha if i understand correctly? you really didn't mean to remove duplicates, if there are duplicates which share the same hotel, group them accordingly, is that correct? check the revision i made – user1978142 Jun 21 '14 at 11:32
  • yes thats what I want to do. your revision is working as I feel but I can't display it using foreach loop. so how can I make it display. Thank You very much for your effort. – Yasitha Jun 22 '14 at 05:13
  • 1
    @Yasitha okay if you want to just echo check the edit – user1978142 Jun 22 '14 at 05:32