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
    )

)
4

5 に答える 5

4

シンプルに保つ...必要なのは:

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

print_r($list); //duplicate removed 
于 2013-03-20T09:05:27.673 に答える
3

これを試して :

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

完全なコードは次のとおりです。

$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
(
    [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
        )

)
于 2013-03-20T09:10:10.303 に答える
0

関数をチェックしてくださいarray_filter()。それはあなたに良い出発点を提供するはずです。

于 2013-03-20T09:04:10.433 に答える
0

これを試して:

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

私のやり方は@PrasanthBendraよりも優れています。オブジェクトが再作成されないからです。:^)しかし、私は@Babaの方法が好きです。

于 2013-03-20T09:15:23.103 に答える
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)));
于 2013-03-20T09:18:40.060 に答える