Reputation: 911
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
}
}
Upvotes: 0
Views: 64
Reputation: 7948
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);
Edit: Simple output
foreach($new_values as $key => $values) {
foreach($values as $element) {
foreach($element as $index => $value) {
echo $index . ' => ' . $value . '<br/>';
}
}
}
Upvotes: 1
Reputation: 796
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
Upvotes: 2