2017-05-24 208 views
0

我有三個鍵指向類型數組的值,在這些數組值中我有重複的值。我怎樣才能刪除它們?刪除多維數組中的重複數據(關聯數組)

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" 
} 
} 
} 
+0

能否請您更具體你想要刪除哪些元素? –

+0

刪除重複項沒有意義,因爲這些重複項位於不同的類別(標記)中。相反,你應該建議一個新的結構來存儲你的數據。 –

+0

我想保留[ROCK],[ALTERNATIVE]和[SAX]的交集值,這些是具有數組類型值的鍵,其中一些值在其他鍵中重複。 – KaldoLeb

回答

2

我會假設,如果兩個頻段具有相同的「身份證」,那麼它們是相同的

$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));末重新索引陣列

+0

這就是我要找的謝謝傑克先生。天才 ;) – KaldoLeb

0

您對official documentation的解決方案......一個人做了一個簡單的功能做你想要什麼。

實例陣列:

<?php 
$details = array( 
    0 => array("id"=>"1", "name"=>"Mike", "num"=>""), 
    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; 
} 
?> 

你必須修復它的工作您的陣列尺寸。

+0

爲什麼downvotes,如果它正是他想要的... – matiaslauriti

+0

當你給一個解決方案,我可能會喜歡這個社區,也許你錯了,沒有人評論,你有-100 downvotes不知道爲什麼,但沒有人給出瞭解決方案... – matiaslauriti

+0

在我的情況下id是[岩石],[替代]和[sax]它來自數據庫 – KaldoLeb