dongliqian6245 2016-12-06 13:11
浏览 26
已采纳

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

I have an array like this

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
                                (
                                )
                        )
                )
        )
)

What I want to from this 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

What I have tried

$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']);
            }
        }
    }

And My output is

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

Edit here is the full array list using var_export so you can just copay and paste

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条回答 默认 最新

  • dongshan1811 2016-12-06 13:21
    关注

    Something like that:

    function getValues($array, $prefix = '')
    {
        $values = [];
    
        foreach ($array as $value) {
            $values[] = $prefix . $value['category_name'];
    
            if (!empty($value['children_recursive'])) {
                $values = array_merge($values, getValues($value['children_recursive'], $prefix . $value['category_name'] . ' >> '));
            }
        }
    
        return $values;
    }
    
    implode('<br>', getValues($array));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备