2014-05-21 59 views
1

print_r($ jobtypes)給我一個數組。數組包含重複的元素。我想刪除重複項。這是一個陣列刪除多維數組中的重複項

   Array 
(
    [0] => stdClass Object 
     (
      [term_id] => 40 
      [name] => Babysitting 
      [slug] => babysitting 
      [term_group] => 0 
      [term_taxonomy_id] => 40 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 3 
     ) 

    [1] => stdClass Object 
     (
      [term_id] => 43 
      [name] => Lawn Mowing 
      [slug] => lawn-mowing 
      [term_group] => 0 
      [term_taxonomy_id] => 43 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 3 
     ) 

    [2] => stdClass Object 
     (
      [term_id] => 39 
      [name] => Leaf Raking 
      [slug] => leaf-raking 
      [term_group] => 0 
      [term_taxonomy_id] => 39 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 2 
     ) 

    [3] => stdClass Object 
     (
      [term_id] => 41 
      [name] => Pet Sitting 
      [slug] => pet-sitting 
      [term_group] => 0 
      [term_taxonomy_id] => 41 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 3 
     ) 

    [4] => stdClass Object 
     (
      [term_id] => 42 
      [name] => Plant Watering 
      [slug] => plant-watering 
      [term_group] => 0 
      [term_taxonomy_id] => 42 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 2 
     ) 

    [5] => stdClass Object 
     (
      [term_id] => 44 
      [name] => Snow Shoveling 
      [slug] => snow-shoveling 
      [term_group] => 0 
      [term_taxonomy_id] => 44 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 5 
     ) 

    [6] => stdClass Object 
     (
      [term_id] => 40 
      [name] => Babysitting 
      [slug] => babysitting 
      [term_group] => 0 
      [term_taxonomy_id] => 40 
      [taxonomy] => job_listing_type 
      [description] => 
      [parent] => 0 
      [count] => 3 
      [filter] => raw 
     ) 

) 

如何刪除重複項。我這種情況下

[name] => Babysitting 
       [slug] => babysitting 
       [term_group] => 0 
       [term_taxonomy_id] => 40 
       [taxonomy] => job_listing_type 
       [description] => 
       [parent] => 0 
       [count] => 3 
       [filter] => raw 

是兩倍。我用

$jobtypes= array_map("unserialize", array_unique(array_map("serialize", $jobtypes))); 

但不工作。謝謝

+0

是什麼語言?請添加標籤。 – zishe

+0

其php語言 – Riz

+0

數據從哪裏來?如果它來自數據庫,則應該編寫查詢,以便重複結果不包含在結果中。另外,你在問之前是否(使用搜索)(https://stackoverflow.com/search?q=%5Bphp%5D+array+remove+duplicates)?有很多[問題](https://stackoverflow.com/questions/3579749/removing-duplicates-from-an-array)基本上要求同樣的事情。 –

回答

2

你可以做一個手動循環結構尋找-每個term_id在一個唯一的數組中存在,如果它不存在然後添加它。

在這個例子中我使用term_id作爲循環期間爲一個簡單的查找陣列密鑰。

$unique = array(); 
foreach($your_array as $key => $value) { 
    // look for non-existance of term_id in $unique array (as key) 
    if(!array_key_exists($value->term_id, $unique)) { 
     // add to unique array 
     $unique[$value->term_id] = $value; 
    } 
} 

你已經創建了一個包含唯一對象的數組後,您可以重置這些數組鍵和獨特的陣列分配回原來的數組是這樣的:

// reset array keys by assigning values to original array 
$your_array = array_values($unique); 
+0

正是我想要的。謝謝 – Riz