2014-05-21 60 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