2013-03-20 16 views
1

안녕하세요, 아래에 표시된 다차원 PHP 배열을 가지고 있습니다. 이 배열에서 중복 된 객체를 제거하기를 원했습니다. 어쨌든 php에서 이것을 수행 할 것인가? 내가 그 반복 stdClass 개체를 제거해야합니다.다차원 배열에서 중복 객체 제거

Array 
(
    [0] => stdClass Object 
     (
      [term_id] => 5 
      [name] => 4x4 
      [slug] => 4x4 
     [term_group] => 0 
     [term_taxonomy_id] => 5 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[1] => stdClass Object 
    (
     [term_id] => 4 
     [name] => Ultra High Performance 
     [slug] => ultra-high-performance 
     [term_group] => 0 
     [term_taxonomy_id] => 4 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[2] => stdClass Object 
    (
     [term_id] => 5 
     [name] => 4x4 
     [slug] => 4x4 
     [term_group] => 0 
     [term_taxonomy_id] => 5 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

[3] => stdClass Object 
    (
     [term_id] => 4 
     [name] => Ultra High Performance 
     [slug] => ultra-high-performance 
     [term_group] => 0 
     [term_taxonomy_id] => 4 
     [taxonomy] => ptd_vehicle_sub_cat 
     [description] => 
     [parent] => 0 
     [count] => 2 
    ) 

) 

답변

3

이 시도 :

$array = array(array(
        "term_id" => 5, 
        "name" => "4x4", 
        "slug" => "4x4", 
        "term_group" => 0, 
        "term_taxonomy_id" => 5, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2, 
       ), 
       array(
        "term_id" => 4, 
        "name" => "Ultra High Performance", 
        "slug" => "ultra-high-performance", 
        "term_group" => 0, 
        "term_taxonomy_id" => 4, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2, 
       ), 
       array(
        "term_id" => 5, 
        "name" => "4x4", 
        "slug" => "4x4", 
        "term_group" => 0, 
        "term_taxonomy_id" => 5, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2 
       ), 
       array(
        "term_id" => 4, 
        "name" => "Ultra High Performance", 
        "slug" => "ultra-high-performance", 
        "term_group" => 0, 
        "term_taxonomy_id" => 4, 
        "taxonomy" => "ptd_vehicle_sub_cat", 
        "description" => 0, 
        "parent" => 0, 
        "count" => 2 
       ) 
); 

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

echo "<pre>"; 
print_r($array); 

출력 :

여기
$array = array_map("unserialize", array_unique(array_map("serialize", $array))); 

는 전체 코드입니다
Array 
(
    [0] => Array 
     (
      [term_id] => 5 
      [name] => 4x4 
      [slug] => 4x4 
      [term_group] => 0 
      [term_taxonomy_id] => 5 
      [taxonomy] => ptd_vehicle_sub_cat 
      [description] => 0 
      [parent] => 0 
      [count] => 2 
     ) 

    [1] => Array 
     (
      [term_id] => 4 
      [name] => Ultra High Performance 
      [slug] => ultra-high-performance 
      [term_group] => 0 
      [term_taxonomy_id] => 4 
      [taxonomy] => ptd_vehicle_sub_cat 
      [description] => 0 
      [parent] => 0 
      [count] => 2 
     ) 

) 
0

array_filter() 기능을 확인하십시오. 그것은 좋은 출발점을 제공해야합니다. 필요

4

간단하게 ... 모두는 다음과 같습니다

$list = array(); 
foreach ($data as $item) { 
    isset($list[$item->term_id]) or $list[$item->term_id] = $item; 
} 

print_r($list); //duplicate removed 
+0

는 기본적으로이 내가 필요로 대답입니다 :

이 그래도 더 나은 방법입니다. 단순한. 감사! –

0

이 시도 :

$array = array_intersect_key($array, array_unique(array_map('serialize', $array))); 

내 방법은 @PrasanthBendra 원인 개체를 다시하지 않습니다보다 낫다. : ^)하지만 나는 바바 길을 선호한다.

0

이 작업을 시도 할 수 :

foreach($array as $key=>$obj) {  
$skey = implode(",",$obj); 
if(!isset($check[$skey])) { 
    $new_array[$key]=$obj; 
    $check[$skey]=1; 
} 
} 

이 중복없이 배열 $ new_array을 떠나야한다.

$array = array_intersect_key($array, array_unique(array_map('serialize', $array)));