删除多维数组(关联数组)中的重复数据

时间:2017-05-24 20:55:45

标签: php arrays duplicates

我有三个键指向类型数组的值,在那些数组值中我有重复的值。我该如何删除它们?

array(3) {
["rock"]=>
array(4) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(15) "Teleman Tickets"
  ["location"]=>
  string(20) "Concorde 2, Brighton"
  ["event_date"]=>
  string(10) "2017-02-20"
  ["event_time"]=>
  string(8) "20:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "50.817321799999990"
  ["geo_long"]=>
  string(17) "-0.12304610000001"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
  ["id"]=>
  string(1) "4"
  ["title"]=>
  string(9) "Blink-182"
  ["location"]=>
  string(25) "Motorpoint Arena, Cardiff"
  ["event_date"]=>
  string(10) "2017-07-03"
  ["event_time"]=>
  string(8) "18:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.478937400000010"
  ["geo_long"]=>
  string(17) "-3.17172289999996"
  ["tags"]=>
  string(14) "rock,metal"
}
[2]=>
array(9) {
  ["id"]=>
  string(1) "8"
  ["title"]=>
  string(5) "Ghost"
  ["location"]=>
  string(29) "O2 Forum Kentish Town, London"
  ["event_date"]=>
  string(10) "2017-03-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.552197000000010"
  ["geo_long"]=>
  string(17) "-0.14196900000002"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
  ["id"]=>
  string(2) "10"
  ["title"]=>
  string(11) "Courteeners"
  ["location"]=>
  string(39) "Emirates Old Trafford, Lancashire C.C.C"
  ["event_date"]=>
  string(10) "2017-05-27"
  ["event_time"]=>
  string(8) "16:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.456428000000000"
  ["geo_long"]=>
  string(17) "-2.28679699999998"
  ["tags"]=>
  string(30) "rock,alternative,indie"
 }
}
["alternative"]=>
array(4) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "1"
  ["title"]=>
  string(15) "Teleman Tickets"
  ["location"]=>
  string(20) "Concorde 2, Brighton"
  ["event_date"]=>
  string(10) "2017-02-20"
  ["event_time"]=>
  string(8) "20:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "50.817321799999990"
  ["geo_long"]=>
  string(17) "-0.12304610000001"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[1]=>
array(9) {
  ["id"]=>
  string(1) "6"
  ["title"]=>
  string(6) "Sum 41"
  ["location"]=>
  string(23) "O2 Academy Leeds, Leeds"
  ["event_date"]=>
  string(10) "2017-02-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.802188400000000"
  ["geo_long"]=>
  string(17) "-1.54713770000001"
  ["tags"]=>
  string(21) "alternative,indie"
}
[2]=>
array(9) {
  ["id"]=>
  string(1) "8"
  ["title"]=>
  string(5) "Ghost"
  ["location"]=>
  string(29) "O2 Forum Kentish Town, London"
  ["event_date"]=>
  string(10) "2017-03-26"
  ["event_time"]=>
  string(8) "19:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "51.552197000000010"
  ["geo_long"]=>
  string(17) "-0.14196900000002"
  ["tags"]=>
  string(30) "rock,alternative,indie"
}
[3]=>
array(9) {
  ["id"]=>
  string(2) "10"
  ["title"]=>
  string(11) "Courteeners"
  ["location"]=>
  string(39) "Emirates Old Trafford, Lancashire C.C.C"
  ["event_date"]=>
  string(10) "2017-05-27"
  ["event_time"]=>
  string(8) "16:00:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "53.456428000000000"
  ["geo_long"]=>
  string(17) "-2.28679699999998"
  ["tags"]=>
  string(30) "rock,alternative,indie"
 }
}
["sax"]=>
array(1) {
[0]=>
array(9) {
  ["id"]=>
  string(1) "3"
  ["title"]=>
  string(9) "Take That"
  ["location"]=>
  string(22) "The SSE Hydro, Glasgow"
  ["event_date"]=>
  string(10) "2017-05-11"
  ["event_time"]=>
  string(8) "18:30:00"
  ["url"]=>
  string(1) "0"
  ["geo_lat"]=>
  string(18) "55.860156000000000"
  ["geo_long"]=>
  string(17) "-4.28525800000000"
  ["tags"]=>
  string(24) "pop,boy-band,sax"
 }
}
}

2 个答案:

答案 0 :(得分:2)

我将假设如果两个乐队具有相同的'id',那么它们是相同的

$unique_bands = array();
// if the master array is called bands    
foreach($bands as $genre){
    foreach($genre as $band){
        $unqiue_bands[$band['id']] = $band;
     }
}

可能会将$unique_bands = array_values(array_filter($unique_bands));放在最后以重新索引数组

答案 1 :(得分:0)

你在official documentation上有一个解决方案......一个人做了一个简单的功能,可以完全按照自己的意愿行事。

示例数组:

<?php 
$details = array( 
    0 => array("id"=>"1", "name"=>"Mike",    "num"=>"9876543210"), 
    1 => array("id"=>"2", "name"=>"Carissa", "num"=>"08548596258"), 
    2 => array("id"=>"1", "name"=>"Mathew",  "num"=>"784581254"), 
); 
?>

功能:

<?php 
function unique_multidim_array($array, $key) { 
    $temp_array = array(); 
    $i = 0; 
    $key_array = array(); 

    foreach($array as $val) { 
        if (!in_array($val[$key], $key_array)) { 
            $key_array[$i] = $val[$key]; 
            $temp_array[$i] = $val; 
        } 
        $i++; 
    } 
    return $temp_array; 
} 
?> 

您必须修复它才能使用数组维度。