2012-10-25 60 views
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是相同的 - 我只是在尋找一種方法來使這個帶回一個獨特的數組(而不是多維)。我看了像this one這樣的文章,但到目前爲止沒有這樣的運氣。有什麼建議麼?謝謝!

+0

對兩個數組級別進行迭代,收集'$ list'中的'term_id's,放棄'$ list'中的''list',將其他元素追加到新的'$ result'數組中。 – mario

回答

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

謝謝,這應該工作! – Zach