如何从数组中合并一些相同的键

时间:2014-06-21 06:08:10

标签: php arrays

我将此数组作为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
    }
}

2 个答案:

答案 0 :(得分:2)

我在stackoverflow找到了这个代码段,这非常有用。

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

以下是原帖的链接,并附有简要说明 https://stackoverflow.com/a/946300/1121121

答案 1 :(得分: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/>';  
        }
    }
}