如何将Multidemensional Tree Array转换为一个简单的数组,以便与Select-Combo-Box一起使用

时间:2016-12-02 11:42:58

标签: php arrays

我正在努力进行PHP中的数组转换。 我有一个多维数组,返回以下内容:

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

1 个答案:

答案 0 :(得分: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非常强大,尽管文档很差。