2012-06-29 25 views
0

我將兩個數組合並在一起,它們都包含一個字符串(url)和int(分數)。以下是外表的一個樣本。每當一個字符串被複制時,我需要刪除該字符串及其相應的int。例如,第4行(www.thebeatles.com/ - 30)應該被刪除。第5行和第6行也應該刪除,因爲它們看起來已經有了不同的分數。二維數組中的尷尬重複php

http://www.thebeatles.com/ - 55 
http://en.wikipedia.org/wiki/The_Beatles - 49 
http://www.beatlesstory.com/ - 45 
http://www.thebeatles.com/ - 30 
http://en.wikipedia.org/wiki/The_Beatles - 28 
http://www.beatlesstory.com/ - 26 
http://www.beatlesagain.com/ - 24 
http://www.thebeatlesrockband.com/ - 23 
http://www.last.fm/music/The+Beatles - 22 
http://itunes.apple.com/us/artist/the-beatles/id136975 - 20 
http://www.youtube.com/watch?v=U6tV11acSRk - 18 
http://blekko.com/ws/http://www.thebeatles.com/+/seo - 17 
http://www.adriandenning.co.uk/beatles.html - 16 
http://www.npr.org/artists/15229570/the-beatles - 15 
http://mp3.com/artist/The%2BBeatles - 14 
http://www.beatles.com/ - 13 
http://www.youtube.com/watch?v=TU7JjJJZi1Q - 12 
http://www.guardian.co.uk/music/thebeatles - 11 
http://www.cirquedusoleil.com/en/shows/love/default.aspx - 9 
http://www.recordingthebeatles.com/ - 7 
http://www.beatlesbible.com/ - 5 

我是PHP的新手,我盡力讓array_unique()工作失敗。真的很感謝一些幫助傢伙!

+0

'$結果=陣列(); foreach($ array as $ val){if(!isset($ result [$ val ['url']])){$ result [$ val ['url']] = $ val; }} print_r($ result);' – DaveRandom

+0

感謝戴夫,我試過你的代碼我認爲它會工作,但我在foreach()中得到了一個未定義的變量錯誤。我應該改變什麼? – shanahobo86

+0

嗯,你需要將'$ array'更改爲你的數組變量的名稱,並且我假設你有一個關聯數組的數組,其中有一個名爲'url'的鍵)。如果你在你的實際數組中顯示一個'print_r()',我可以給你一個有保證的工作解決方案 - 但我不應該真的只是給你一個盤子上的答案,我們喜歡它,如果人們把一個* *努力在自己...... ;-)。我使用URL作爲結果數組中的關鍵字,以便輕鬆地進行重複檢測,您可能希望通過'array_values()'傳遞'$ result'使其重新索引。 – DaveRandom

回答

0

這裏是合併兩個陣列和丟棄任何重複的功能,希望它有助於:

 function merge_links($arr_l, $arr_r) { 
      $new_links = array(); 
      $links = array_merge($arr_l, $arr_r); //the big list with every links 


      foreach($links as $link) { 
       $found = false; //did we found a duplicate? 

       //check if we already have it 
       foreach($new_links as $new_link) { 
        if($new_link['url'] == $link['url']) { 
         //duplicate 
         $found = true; 

         break; 
        } 
       } 

       //not found, so insert it 
       if(!$found) { 
        $new_links[] = $link; 
       } 
      } 

      return $new_links; 
     } 

     $arr1[0]['url'] = 'http://test.nl'; 
     $arr1[0]['score'] = 30; 

     $arr1[1]['url'] = 'http://www.google.nl'; 
     $arr1[1]['score'] = 30; 

     $arr2[0]['url'] = 'http://www.tres.nl'; 
     $arr2[0]['score'] = 30; 

     $arr2[1]['url'] = 'http://test.nl'; 
     $arr2[1]['score'] = 30; 

     print_r(merge_links($arr1, $arr2)); 
0

您可以將鏈接作爲包含鏈接和分數的數組的關鍵字。對應於鍵總會有一個值。但是最後添加的那個將會在你的最終陣列中出現。

0

那麼,即使在技術上,這些字符串並不是唯一的。即它們完全不同。

所以,array_unique()不會給你所需要的輸出。解決這個問題的一種方法是定義一個單獨的數組並分別存儲URI和數字。一個可管理的形式就是這個。

array(

    array("http://www.thebeatles.com", 55), 
    array("http://www.thebeatles.com", 30) 

); 
+0

嘿謝謝你的回覆。這是一個二維數組。要打印結果,代碼如下: > > for($ x = sizeof($ result); $ x> = 0; $ x - url']。「 - 」。$ result [$ x] ['score']。「
」; > 我只是認爲有一些方法可以檢測數組中的字符串部分的重複,然後刪除所有包括相關號碼 – shanahobo86

+0

@ user1487983,還有其他方法,您可以使用正則表達式檢測唯一鏈接。你有鏈接和號碼作爲單獨的參數?或者你在刮HTML文本? – Starx