使用数组中的递归函数获取一行中的所有数据

时间:2016-12-06 13:11:56

标签: php arrays recursion

我有一个像这样的数组

Array
(
    [0] => Array
        (
            [id] => 1
            [category_name] => One
            [parent_id] => 
            [children_recursive] => Array
                (
                )
        )
    [1] => Array
        (
            [id] => 2
            [category_name] => Two
            [parent_id] =>
            [children_recursive] => Array
                (
                )
        )
    [2] => Array
        (
            [id] => 3
            [category_name] => Three
            [parent_id] => 
            [children_recursive] => Array
                (
                    [0] => Array
                        (
                            [id] => 17
                            [category_name] => Three and one
                            [parent_id] => 3
                            [children_recursive] => Array
                                (
                                )
                        )
                    [1] => Array
                        (
                            [id] => 19
                            [category_name] => Three and two
                            [parent_id] => 3
                            [children_recursive] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 21
                                            [category_name] => Three and two and one
                                            [parent_id] => 19
                                            [children_recursive] => Array
                                                (
                                                )
                                        )
                                )
                        )
                    [2] => Array
                        (
                            [id] => 20
                            [category_name] => Three and three
                            [parent_id] => 3
                            [children_recursive] => Array
                                (
                                )
                        )
                )
        )
    [3] => Array
        (
            [id] => 4
            [category_name] => Four
            [parent_id] => 
            [children_recursive] => Array
                (
                    [0] => Array
                        (
                            [id] => 18
                            [category_name] => Four and one
                            [parent_id] => 4
                            [children_recursive] => Array
                                (
                                )
                        )
                )
        )
)

我想从这个阵列中得到什么

One
Two
Three
Three >> Three and one
Three >> Three and two
Three >> Three and two >> Three and two and one
Three >> Three and three
Four
Four >> Four and one

我尝试了什么

$category = myarray;
renderNode($category);

function renderNode($node) {
        foreach($node as $cat){
            echo $cat['category_name'].'<br >';
            if(!empty($cat[children_recursive'])){
                renderNode($cat[children_recursive']);
            }
        }
    }

我的输出是

One
Two
Three
Three and one
Three and two
Three and two and one
Three and three
Four
Four and one

修改 这是使用var_export的完整数组列表,因此您可以直接使用并粘贴

array (
  0 => 
  array (
    'id' => 1,
    'category_name' => 'One',
    'parent_id' => NULL,
    'children_recursive' => 
    array (
    ),
  ),
  1 => 
  array (
    'id' => 2,
    'category_name' => 'Two',
    'parent_id' => NULL,
    'children_recursive' => 
    array (
    ),
  ),
  2 => 
  array (
    'id' => 3,
    'category_name' => 'Three',
    'parent_id' => NULL,
    'children_recursive' => 
    array (
      0 => 
      array (
        'id' => 17,
        'category_name' => 'Three and one',
        'parent_id' => 3,
        'children_recursive' => 
        array (
        ),
      ),
      1 => 
      array (
        'id' => 19,
        'category_name' => 'Three and two',
        'parent_id' => 3,
        'children_recursive' => 
        array (
          0 => 
          array (
            'id' => 21,
            'category_name' => 'Three and two and one',
            'parent_id' => 19,
            'children_recursive' => 
            array (
            ),
          ),
        ),
      ),
      2 => 
      array (
        'id' => 20,
        'category_name' => 'Three and three',
        'parent_id' => 3,
        'children_recursive' => 
        array (
        ),
      ),
    ),
  ),
  3 => 
  array (
    'id' => 4,
    'category_name' => 'Four',
    'parent_id' => NULL,
    'children_recursive' => 
    array (
      0 => 
      array (
        'id' => 18,
        'category_name' => 'Four and one',
        'parent_id' => 4,
        'children_recursive' => 
        array (
        ),
      ),
    ),
  ),
)

3 个答案:

答案 0 :(得分:1)

类似的东西:

java.lang.IllegalStateException: Haven't seen any document yet.

答案 1 :(得分:1)

您可以使用RecursiveArrayIterator。你需要像这样扩展它:

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

    public function getChildren()
    {
        return new static($this->current()['children_recursive']);
    }
}

有了这个课程,您只需循环使用一个foreach

$iterator = new RecursiveIteratorIterator(
    new RecursiveChildrenIterator($array),
    RecursiveIteratorIterator::SELF_FIRST  
);

$result = [];
$current = [];
foreach ($iterator as $item) {
    $current[$iterator->getDepth()] = $item['category_name'];

    if (!$iterator->hasChildren()) {
        $result[] = implode(
            '>>',
            array_slice($current, 0, $iterator->getDepth() + 1)
        );
    }
}

通知传递给RecursiveIteratorIteratorRecursiveIteratorIterator::SELF_FIRST标记。

这是working demo

<强>增加:

我实际上误读了你想要的数组,所以为了得到你想要的那个删除条件:

foreach ($iterator as $item) {
    $current[$iterator->getDepth()] = $item['category_name'];

    $result[] = implode(
        '>>',
        array_slice($current, 0, $iterator->getDepth() + 1)
    );
} 

这是working demo

您可以阅读有关iterators的更多信息。

答案 2 :(得分:0)

$category = myarray;
renderNode($category);

function renderNode($parent_category_name = '' ,$node) {
        foreach($node as $cat){
            echo $parent_category_name.$cat['category_name'].'<br >';
            if(!empty($cat[children_recursive'])){
                renderNode($parent_category_name .$cat['category_name'] ." >> ", $cat[children_recursive']);
            }
        }
    }