为了账号安全,请及时绑定邮箱和手机立即绑定

从上到下循环进入多维数组

从上到下循环进入多维数组

PHP
拉丁的传说 2023-11-03 20:17:12
我有这棵树:Array(    [0] => Array        (            [id] => 1            [parent_id] => 0            [title] => Parent Page            [children] => Array                        (                            [0] => Array                                (                                    [id] => 2                                    [parent_id] => 1                                    [title] => Sub Page                                                                                    ),                            [1] => Array                                (                                    [id] => 5                                    [parent_id] => 1                                    [title] => Sub Page 2                                                                                     )                        )        )    [1] => Array        (            [id] => 4            [parent_id] => 0            [title] => Another Parent Page        ))我正在寻找从上到下的显示。并显示类似这样的内容:1 1.21.54但如果我有 id 3,它是 5 的叶子,我会喜欢这样:11.21.51.5.34我进行了很多搜索,当我使用递归时,我的大脑是有限的。我已经尝试过这个:function printAll($a){  foreach ($a as $v){      if (!array_key_exists('children', $v)){          debugLog($v['id']);          return;      }      else{          $arrayChildrens = $v['children'];          foreach($arrayChildrens as $c){              $arrayChildrens = $c['children'];              $this->printAll($arrayChildrens);          }      }  }}但不起作用..我尝试开始只是为了显示1254但我的目标是在 id 之前显示 id 父母(就像我之前向你展示的那样)多谢 !
查看完整描述

2 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

这个函数应该会给你你期望的输出。


function printAll($a, $prefix = '') {


    //loop through $a

    foreach($a as $v) {


        //echo current level `id` with previous `$prefix`

        echo "{$prefix}{$v['id']}\n";


        //check if current level contains children

        if(!empty($v['children'])) {


            //clean up prefix to remove extra `.` at the end of prefixes

            $prev_prefix = rtrim($prefix, '.');


            //recurse printAll again passing the children as `$a` and a `$prefix` being the previous levels combined e.g `1.5`

            //also clean up extra periods at the start of the prefix

            printAll($v['children'], ltrim("{$prev_prefix}.{$v['id']}.", "."));

        }

    }

}

输出:


1

1.2

1.5

1.5.3

4

使用正确的回报

通常,对于一个函数,您实际上希望该函数返回值,而不是自动将它们回显到您的页面。如果您希望此函数返回一个值数组而不是回显它们,您可以这样做:


function printAll($a, $level = '', $values = []) {

    foreach($a as $v) {

        $values[] = $value = "{$level}{$v['id']}";

        if(!empty($v['children'])) {

            $values = printAll($v['children'], "{$value}.", $values);

        }

    }

    return $values;

}

将会得到这样的结果:


Array

(

    [0] => 1

    [1] => 1.2

    [2] => 1.5

    [3] => 1.5.3

    [4] => 4

)


查看完整回答
反对 回复 2023-11-03
?
MYYA

TA贡献1868条经验 获得超4个赞

这应该可以完成工作。


$arr = array(

    array(

        'id' => 1,

        'parent_id' => 0,

        'title' => 'Parent Page',

        'children' => array(

            array(

                'id' => 2,

                'parent_id' => 1,

                'title' => 'Sub Page',

            ),

            array(

                'id' => 5,

                'parent_id' => 1,

                'title' => 'Sub Page 2',

                'children' => array(

                    array(

                        'id' => 7,

                        'parent_id' => 5,

                        'title' => 'Sub Page',

                    ),

                    array(

                        'id' => 8,

                        'parent_id' => 5,

                        'title' => 'Sub Page 2',

                    )

                )

            )

        )

    ),

    array(

        'id' => 4,

        'parent_id' => 0,

        'title' => 'Another Parent Page',

    )

);

function printAll($arr, $parent = [])

{

    if (is_array($arr)) {

        foreach ($arr as $k => $v) {

            if (isset($v['id'])) {

                $parent[] = $v['id'];

                echo implode('.', $parent) . PHP_EOL;

            }

            if (isset($v['children'])) {

                printAll($v['children'], $parent);

            }

            array_pop($parent);

        }

    }

}

printAll($arr);

输出


1

1.2

1.5

1.5.7

1.5.8

4

工作演示



查看完整回答
反对 回复 2023-11-03
  • 2 回答
  • 0 关注
  • 69 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信