从多维数组中删除重复项

时间:2014-05-21 01:26:39

标签: php arrays

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)));

但没有工作。感谢

1 个答案:

答案 0 :(得分: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);