从多维数组中获取唯一数组

时间:2012-10-25 20:48:19

标签: php arrays

我有以下数组:

Array
(
    [0] => Array
        (
            [126] => stdClass Object
                (
                    [term_id] => 126
                    [name] => Pine
                    [slug] => pine
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 129
                    [taxonomy] => wood_types
                    [description] => Pine
                    [parent] => 0
                    [count] => 6
                    [object_id] => 890
                )

            [131] => stdClass Object
                (
                    [term_id] => 131
                    [name] => PVC
                    [slug] => pvc
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 134
                    [taxonomy] => wood_types
                    [description] => PVC
                    [parent] => 0
                    [count] => 6
                    [object_id] => 890
                )

            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 890
                )

        )

    [1] => Array
        (
            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 865
                )

        )

    [2] => Array
        (
            [125] => stdClass Object
                (
                    [term_id] => 125
                    [name] => Oak
                    [slug] => oak
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 128
                    [taxonomy] => wood_types
                    [description] => Oaker
                    [parent] => 0
                    [count] => 3
                    [object_id] => 871
                )

        )

    [3] => Array
        (
            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 867
                )

        )

    [4] => Array
        (
            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 864
                )

        )

    [5] => Array
        (
            [125] => stdClass Object
                (
                    [term_id] => 125
                    [name] => Oak
                    [slug] => oak
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 128
                    [taxonomy] => wood_types
                    [description] => Oaker
                    [parent] => 0
                    [count] => 3
                    [object_id] => 771
                )

            [126] => stdClass Object
                (
                    [term_id] => 126
                    [name] => Pine
                    [slug] => pine
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 129
                    [taxonomy] => wood_types
                    [description] => Pine
                    [parent] => 0
                    [count] => 6
                    [object_id] => 771
                )

            [131] => stdClass Object
                (
                    [term_id] => 131
                    [name] => PVC
                    [slug] => pvc
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 134
                    [taxonomy] => wood_types
                    [description] => PVC
                    [parent] => 0
                    [count] => 6
                    [object_id] => 771
                )

        )

    [6] => 
    [7] => Array
        (
            [126] => stdClass Object
                (
                    [term_id] => 126
                    [name] => Pine
                    [slug] => pine
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 129
                    [taxonomy] => wood_types
                    [description] => Pine
                    [parent] => 0
                    [count] => 6
                    [object_id] => 894
                )

            [131] => stdClass Object
                (
                    [term_id] => 131
                    [name] => PVC
                    [slug] => pvc
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 134
                    [taxonomy] => wood_types
                    [description] => PVC
                    [parent] => 0
                    [count] => 6
                    [object_id] => 894
                )

        )

    [8] => Array
        (
            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 873
                )

        )

    [9] => Array
        (
            [137] => stdClass Object
                (
                    [term_id] => 137
                    [name] => Special Order
                    [slug] => special-order
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 143
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 6
                    [object_id] => 869
                )

        )

    [10] => Array
        (
            [143] => stdClass Object
                (
                    [term_id] => 143
                    [name] => base
                    [slug] => base-2
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 149
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 0
                    [count] => 1
                    [object_id] => 892
                )

            [138] => stdClass Object
                (
                    [term_id] => 138
                    [name] => Test Child
                    [slug] => test-child
                    [term_group] => 0
                    [term_order] => 0
                    [term_taxonomy_id] => 144
                    [taxonomy] => wood_types
                    [description] => 
                    [parent] => 126
                    [count] => 1
                    [object_id] => 892
                )

        )

    [11] => 
)

这些来自单个项目,它们会附加一系列术语对象(这就是为什么有些项目有多个条目)。你可以看到,有许多term_id是相同的 - 我只是想找到一种方法来使这个带回一个独特的数组(而不是多维)。我查看了像this one这样的文章,但到目前为止还没有这样的运气。有什么建议?谢谢!

1 个答案:

答案 0 :(得分:0)

这样的事情?

$out = array();

foreach($data as $row){
    if(is_array($row)){
        foreach($row as $id=>$term){
            if(!isset($out[$id])){
                $out[$id] = $term;
            }
        }
    }
}
print_r($out);