2012-11-26 54 views
0

我有對象的樹:陣列樹

array(4) (
    0 => object stdClass(14) { 
     public id => string(1) "1" 
     public parent_id => string(1) "0" 
     public name => string(18) "Stationary engines" 
     public uri => string(18) "stationary-engines" 
     public created => string(19) "2012-11-19 15:15:34" 
     public updated => NULL 
     public subcategories => array(4) (
      0 => object stdClass(14) { 
       public id => string(1) "5" 
       public parent_id => string(1) "1" 
       public name => string(6) "Yanmar" 
       public uri => string(6) "yanmar" 
       public created => string(19) "2012-11-19 15:23:36" 
       public updated => NULL 
       public subcategories => array(1) (
        0 => object stdClass(14) { 
         public id => string(2) "15" 
         public parent_id => string(1) "5" 
         public name => string(18) "Yanmar subcategory" 
         public uri => string(18) "yanmar-subcategory" 

         public created => string(19) "2012-11-21 16:38:06" 
         public updated => NULL 
         public subcategories => array(1) (
          0 => object stdClass(13) { 
           public id => string(2) "16" 
           public parent_id => string(2) "15" 
           public name => string(30) "Yanmar subcategory subcategory" 
           public uri => string(30) "yanmar-subcategory-subcategory" 
           public created => string(19) "2012-11-21 17:37:00" 
           public updated => NULL 
          } 
         ) 
        } 
       ) 
      } 
      1 => object stdClass(13) { 
       public id => string(1) "6" 
       public parent_id => string(1) "1" 
       public name => string(11) "Мercruiser" 
       public uri => string(10) "mercruiser" 
       public created => string(19) "2012-11-19 15:23:36" 
       public updated => NULL 
      } 
      2 => object stdClass(13) { 
       public id => string(1) "7" 
       public parent_id => string(1) "1" 
       public name => string(11) "Volvo-Penta" 
       public uri => string(11) "volvo-penta" 
       public created => string(19) "2012-11-19 15:24:49" 
       public updated => NULL 
      } 
      3 => object stdClass(13) { 
       public id => string(1) "8" 
       public parent_id => string(1) "1" 
       public name => string(27) "Basic configuration engines" 
       public uri => string(27) "basic-configuration-engines" 
       public created => string(19) "2012-11-19 15:24:49" 
       public updated => NULL 
      } 
     ) 
    } 
    1 => object stdClass(14) { 
     public id => string(1) "2" 
     public parent_id => string(1) "0" 
     public name => string(10) "Generators" 
     public uri => string(10) "generators" 
     public created => string(19) "2012-11-19 15:15:58" 
     public updated => NULL 
     public subcategories => array(4) (
      0 => object stdClass(13) { 
       public id => string(1) "9" 
       public parent_id => string(1) "2" 
       public name => string(6) "Diesel" 
       public uri => string(6) "diesel" 
       public created => string(19) "2012-11-19 15:34:38" 
       public updated => NULL 
      } 
      1 => object stdClass(13) { 
       public id => string(2) "10" 
       public parent_id => string(1) "2" 
       public name => string(8) "Gasoline" 
       public uri => string(8) "gasoline" 
       public created => string(19) "2012-11-19 15:34:38" 
       public updated => NULL 
      } 
      2 => object stdClass(13) { 
       public id => string(2) "11" 
       public parent_id => string(1) "2" 
       public name => string(6) "Kohler" 
       public uri => string(6) "kohler" 
       public created => string(19) "2012-11-19 15:35:35" 
       public updated => NULL 
      } 
      3 => object stdClass(13) { 
       public id => string(2) "12" 
       public parent_id => string(1) "2" 
       public name => string(13) "Fischer Panda" 
       public uri => string(13) "fischer-panda" 
       public created => string(19) "2012-11-19 15:36:14" 
       public updated => NULL 
      } 
     ) 
    } 
    2 => object stdClass(14) { 
     public id => string(1) "3" 
     public parent_id => string(1) "0" 
     public name => string(14) "Boat equipment" 
     public uri => string(14) "boat-equipment" 
     public created => string(19) "2012-11-19 15:16:59" 
     public updated => NULL 
     public subcategories => array(1) (
      0 => object stdClass(13) { 
       public id => string(2) "13" 
       public parent_id => string(1) "3" 
       public name => string(11) "Subcategory" 
       public uri => string(11) "subcategory" 
       public created => string(19) "2012-11-19 15:37:12" 
       public updated => NULL 
      } 
     ) 
    } 
    3 => object stdClass(14) { 
     public id => string(1) "4" 
     public parent_id => string(1) "0" 
     public name => string(11) "Spare parts" 
     public uri => string(11) "spare-parts" 
     public created => string(19) "2012-11-19 15:16:59" 
     public updated => NULL 
     public subcategories => array(1) (
      0 => object stdClass(13) { 
       public id => string(2) "14" 
       public parent_id => string(1) "4" 
       public name => string(11) "Consumables" 
       public uri => string(11) "consumables" 
       public created => string(19) "2012-11-19 15:37:39" 
       public updated => NULL 
      } 
     ) 
    } 
) 

,我需要將其轉換爲關聯數組是這樣的:

array(
    '1' => 'category name', 
    '3' => '   subcategory name', 
    '4' => '      subcategory name', 
    '2' => 'category2 name', 
    '5' => '   subcategory2 name', 
); 

每個子類別應縮進空間。 但是我對遞歸併不是很熟悉,需要一些幫助,如果你能幫助我,我會非常感激。 在此先感謝。

更新: 我已經結束了以下內容:

$options = array(); 
$this->to_array($tree, $options, '   ', 0); 

function to_array($tree, & $target, $indention = '', $lvl = 0) 
{ 
    foreach($tree as $leaf) 
    { 
     $target[$leaf->id] = str_repeat($indention, $lvl).$leaf->name; 

     if (isset($leaf->subcategories)) 
     { 
      $this->to_array($leaf->subcategories, $target, $indention, $lvl + 1); 
     } 
    } 
} 

上改進建議?

+0

如果所有的密鑰都是id,你將如何從最終數組中獲取第n個ID? – Dale

+0

爲什麼你想這樣,所有的數組都是相同的結構,所以用於每個循環來循環數據。 –

+0

我不認爲你可以像這樣完成它。每個'id'鍵將覆蓋前面的'id'鍵。 –

回答

1

我會去沿着線的東西:

function printArray($arr, &$target, $header = '') { 
    foreach($arr as $category) { 
    $target[$category->id] = $header . $category->name; 
    printArray($category->subcategories, $target, $header . ' '); 
    } 
} 

// And then, from your source 
$targetArray = array(); 
printArray($source, $targetArray); 

這不應對可能出現的衝突,但如果你的數據從一個表來,則衝突是不應該的。

要定製甚至更多,你通過頭部修改作爲參數傳遞給像遞歸函數:

function printArray($arr, &$target, $header = '', $headerIncrement = ' ') { 
    foreach($arr as $category) { 
    $target[$category->id] = $header . $category->name; 
    printArray($category->subcategories, $target, $header . $headerIncrement, $headerIncrement); 
    } 
} 

這將讓你與

printArray($source, $target, '', '   '); 
//or even 
printArray($source, $target, '', '<span class="spacer">&nbsp;</span>'); 

調用它,並且在添加樣式您css來改變你想要的間距。

+0

謝謝,你已經給我看了燈!我更新了問題,也許你可以對此代碼進行一些改進? – UAMoto

+0

我只是更新我的代碼,我忘記了在遞歸調用中更新'$ header'。現在'$ header'會隨着每個級別的增長而增長。這會爲每個級別添加「&nbsp」。您可以通過更改遞歸調用來改變行爲(從'$ header。'&nbsp''到'$ header。'   '')。 – jaudette

+0

我在我的問題中使用的代碼,再次感謝。 – UAMoto