0

我有以下数组:

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是相同的——我只是在寻找一种方法来使它带回一个唯一的数组(而不是多维的)。我看过这样的文章但到目前为止还没有这样的运气。有什么建议么?谢谢!

4

1 回答 1

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);
于 2012-10-25T20:55:07.377 回答