2016-12-02 8 views
2

我很努力地在PHP中進行數組轉換。 我有一個多維數組,返回如下:如何將多維樹陣列轉換爲一個簡單的數組以供Select-Combo-Box使用

var_export($ ARR)

$array = array (
    0 => 
    array (
    'value' => '54', 
    'menu_title' => 'EN', 
    'page_code' => '54', 
    'icon' => 'EN', 
    'selected' => '', 
), 
    1 => 
    array (
    'parent' => 
    array (
     0 => 
     array (
     'value' => '51', 
     'menu_title' => '-- Gallery', 
     'page_code' => '-- 51', 
     'icon' => 'none', 
     'selected' => '', 
    ), 
     1 => 
     array (
     'value' => '56', 
     'menu_title' => '-- -- another one', 
     'page_code' => '-- -- 56', 
     'icon' => 'none', 
     'selected' => '', 
    ), 
     2 => 
     array (
     'parent' => 
     array (
      0 => 
      array (
      'value' => '59', 
      'menu_title' => '-- -- child of another one', 
      'page_code' => '-- -- 59', 
      'icon' => 'none', 
      'selected' => '', 
     ), 
      1 => 
      array (
      'parent' => 
      array (
      ), 
     ), 
     ), 
    ), 
    ), 
), 
); 
然而

,我需要它轉換爲是這樣的: (的var_dump)

Array 
(

      [0] => Array 
       (
        [value] => 54 
        [menu_title] => EN 
        [page_code] => 54 
        [icon] => EN 
        [selected] => 
       ) 


      [1] => Array 
       (
        [value] => 51 
        [menu_title] => -- Gallery 
        [page_code] => -- 51 
        [icon] => none 
        [selected] => 
       ) 

      [2] => Array 
       (
        [value] => 56 
        [menu_title] => -- -- another one 
        [page_code] => -- -- 56 
        [icon] => none 
        [selected] => 
       ) 


      [3] => Array 
       (
        [value] => 59 
        [menu_title] => -- -- child of another one 
        [page_code] => -- -- 59 
        [icon] => none 
        [selected] => 
       ) 
) 

我從昨天開始就坐在這個問題上,不知何故我不能讓它工作 這應該。

感謝您的任何提示。 Steffano

+0

請代替'var_dump'使用'var_export'作爲第一個數組,因此它可以被複制和粘貼。 – sevavietl

+0

前面的數組有不同的結構。另外,當你有'parent_id'時,我不知道爲什麼你需要嵌套。 – sevavietl

+0

使用之前的數組結構,可以執行[this](http://sandbox.onlinephpfunctions.com/code/ed6e9a88ad460d67bbd646da9049a3cfd8651300)。但是,您的數據表示顯然存在問題。所以我建議你可以重新考慮數組結構。 – sevavietl

回答

1

當我需要迭代嵌套數組時,首先想到的是RecursiveArrayIterator。因爲我們有複雜的數組,葉子的非平凡規則(默認規則,據我瞭解,無論數組元素是否爲數組),我們需要擴展RecursiveArrayIterator並重寫兩種方法:hasChildrengetChildren

class CustomRecursiveArrayIterator extends RecursiveArrayIterator 
{ 
    public function hasChildren() 
    { 
     return !empty($this->current()['parent']); 
    } 

    public function getChildren() 
    { 
     $children = array_filter($this->current()['parent'], function ($child) { 
      return !isset($child['parent']) || !empty($child['parent']); 
     }); 

     return new static($children); 
    } 
} 

然後我們就可以迭代器在這個迭代器並收集所有的葉子:

$iterator = new RecursiveIteratorIterator(
    new CustomRecursiveArrayIterator($array) 
); 

$leafs = []; 
foreach ($iterator as $leaf) { 
    $leafs[] = $leaf; 
} 

採取的通知,這是可能的,因爲RecursiveIteratorIterator有模式RecursiveIteratorIterator::LEAVES_ONLY,這是默認設置。

這裏是working demo

正如你所見,iterators功能非常強大,雖然文檔很差。

+0

感謝您的澄清。的確非常有用。我以某種方式試圖避免這些類,正如你所說的那樣,它們沒有很好的文檔記錄,我並不完全「得到」它。但是你的澄清是非常有幫助的,我開始明白這些課程真的有多強大。 親切的問候,埃斯特法諾 – Steffano

+0

@Steffano,那麼你可以標記這是正確的答案;) – sevavietl

+0

我該怎麼做?有沒有按鈕?我對這個平臺很處女。 編輯:哦,我想我明白了,這是綠色箭頭,對吧? :-P – Steffano

相關問題