0

I have the following array:

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] => 
)

These are coming from individual items that bring back an array of term objects attached to it (so that's why some have multiple entries). You can see throughout, there are a number of term_id's that are the same -- and I'm simply looking for a way to make this bring back a unique array (and not be multidimensional). I looked at articles like this one, but no such luck so far. Any suggestions? Thanks!

Community
  • 1
  • 1
Zach
  • 1,185
  • 3
  • 24
  • 60
  • Iterate over both array levels, collect the `term_id`s in a `$list`, drop the ones already in the `$list`, append the others on a new `$result` array. – mario Oct 25 '12 at 20:51

1 Answers1

0

something like this?

$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);
Jonathan Kuhn
  • 15,279
  • 3
  • 32
  • 43