Php 显示多级递归创建的数组

Php 显示多级递归创建的数组,php,arrays,recursion,Php,Arrays,Recursion,我创建了一个数组,它包含多个级别的“子”数组 $this->project_tasks: Array ( [0] => Array ( [person_name] => User 1 [reports_to_id] => [task_id] => 1123 [task_name] => Main Task [sub_task_id] => Array

我创建了一个数组,它包含多个级别的“子”数组

$this->project_tasks: Array
(
[0] => Array
    (
        [person_name] => User 1
        [reports_to_id] => 
        [task_id] => 1123
        [task_name] => Main Task
        [sub_task_id] => Array
            (
                [0] => Array
                    (
                        [person_name] => User 1
                        [reports_to_id] => 1123
                        [task_id] => 1124
                        [task_name] => sub Task 1-1
                        [sub_task_id] => Array
                            (
                                [0] => Array
                                    (
                                        [person_name] => User 1
                                        [reports_to_id] => 1124
                                        [task_id] => 1125
                                        [task_name] => sub Task 1-1-1

                                    )

                            )

                    )

                [1] => Array
                    (
                        [person_name] => User 1
                        [reports_to_id] => 1123
                        [task_id] => 1127
                        [task_name] => sub Task 1-2
                    )

            )

    )

[1] => Array
    (
        [person_name] => User 1
        [reports_to_id] => 
        [task_id] => 1126
        [task_name] => Main Task 2
    )

)
创建数组没有问题,当前的问题是如何迭代整个数据集,使其显示为:

1123 - Main Task
  1124 - sub Task 1-1
    1125 - sub Task 1-1-1
  1127 - sub Task 1-2
  ...
1126 - Main Task 2
  ...
etc...
我找到了array\u walk\u recursive(),但它不会处理包含数组的键。此外,数组结构必须不关心递归的“深度”。可能是10+级,没关系

非常感谢您的帮助。

请尝试以下代码:

function customRecursiveWalk($array,$level){
    $size = count($array);

    for ($i=0;$i<$size;$i++) {

        for ($j=0;$j<$level;$j++)
            echo "&nbsp;&nbsp;"; //Print the indentation level

        //Print the current task id and the task name
        echo($array[$i]['task_id'] ." - ".$array[$i]['task_name']);
        echo '<br/>';

        //Call the recursive walk for the child
        customRecursiveWalk($array[$i]['sub_task_id'],$level+1);
    }
}

customRecursiveWalk($this->project_tasks,0);
函数customRecursiveWalk($array,$level){
$size=计数($array);
对于($i=0;$i项目任务,0);

我不知道完整的用例,但似乎一个简单的递归函数就可以做到这一点。我使用了ul/li来建立多个级别,但这当然不是必需的——例如,我还包括了一个函数,它将为您提供一个按顺序列出的任务的平面数组

首先,HTML版本:

function listTasks($list, $level=0) {
    print '<li>'.$list['task_id'].' - '.$list['task_name'];
    if (array_key_exists('sub_task_id', $list)) {
        print '<br><ul>';
        foreach ($list['sub_task_id'] as $subtasks) {
            listTasks($subtasks, $level+1);
        }
        print '</ul>';
    }
    print '</li>';
}
print '<ul>';
foreach ($a as $tasks) {
    listTasks($tasks, 1);
}
print '</ul>';
生成平面阵列:

function listTasks($list, &$output, $level=0) {
    $output[] = array(
        'id'=>$list['task_id'],
        'name'=>$list['task_name'],
        'level'=>$level
    );
    if (array_key_exists('sub_task_id', $list)) {
        foreach ($list['sub_task_id'] as $subtasks) {
            listTasks($subtasks, $output, $level+1);
        }
    }
}
$output = array();
foreach ($a as $tasks) {
    listTasks($tasks, $output);
}
输出:

Array
(
    [0] => Array
        (
            [id] => 1123
            [name] => Main Task
            [level] => 0
        )    
    [1] => Array
        (
            [id] => 1124
            [name] => sub Task 1-1
            [level] => 1
        )    
    [2] => Array
        (
            [id] => 1125
            [name] => sub Task 1-1-1
            [level] => 2
        )    
    [3] => Array
        (
            [id] => 1127
            [name] => sub Task 1-2
            [level] => 1
        )    
    [4] => Array
        (
            [id] => 1126
            [name] => Main Task 2
            [level] => 0
        )    
)

您可以使用适用于数组数据的RecursiveIteratorIterator,但是RecursiveIteratorIterator不提供特定数组的深度。当将一个级别“向上”移动到正在显示的数组的新同级数组时,这会成为一个问题。关闭,非常接近。如果我能让它工作,我将发布最终解决方案。这是什么这段代码有问题吗?也许我可以试着去解决它。这不是一件事,是我度过了糟糕的一天。经过一些轻微的更改后,它工作得非常完美。TY@Fábio Hiroki。现在删除列表元素并用表行替换它……TYVM@Chris。希望这就是我想要的。
Array
(
    [0] => Array
        (
            [id] => 1123
            [name] => Main Task
            [level] => 0
        )    
    [1] => Array
        (
            [id] => 1124
            [name] => sub Task 1-1
            [level] => 1
        )    
    [2] => Array
        (
            [id] => 1125
            [name] => sub Task 1-1-1
            [level] => 2
        )    
    [3] => Array
        (
            [id] => 1127
            [name] => sub Task 1-2
            [level] => 1
        )    
    [4] => Array
        (
            [id] => 1126
            [name] => Main Task 2
            [level] => 0
        )    
)