简体   繁体   中英

PHP recursion array using MYSQL data

I have a table with data, this data is represented as rows each row is a task, a task might have a subtask and that subtask might have a subtask as well. This represents, to make it easier vinyl records from when you first buy it, then if you return it as well.

Here is my table:

-----------------------------------------------------------------------------
id | task_name  | parent_task_id
-----------------------------------------------------------------------------
1  | start      | 0
2  | buy        | 1
3  | receive    | 2
4  | open       | 2
5  | clean      | 4
6  | listen     | 4
7  | file       | 4
8  | return     | 1
9  | pack       | 8
10 | send       | 8
11 | side A     | 6
12 | side B     | 6

First of all I know the table design is not great at all but I can't change that.

For example the first row has no parent, its at 'start' that is where tree starts.

Here is the tree visually that would like to represent in PHP with a recursive function. Bonus points if you can find one using MYSQL query as well.

start -> buy    -> receive
                -> open ------> clean
                        ------> listen -> side A
                                       -> side B
                        ------> file   

      -> return -> pack
                -> send

As you can see its a tree that has a parent and first 2 children (can have more) and then those children have their own children and so on. Basically would like to create a recursive function that can do this.

Here my code so far, just can't get the whole recursion part and where it should go:

function get_project($options, $array_data=0) { // maybe pass array

    $tasks = $this->get_data(); // gets data from database

    $subtasks = array();

        foreach($tasks as $task_id => $task) { 
            if($task->parent_task_id > 0 ) { 
                $subtasks[] = $task; 
                unset($tasks[$task_id]); 
            }
        }

        foreach($tasks as $task) { 
                $subtasks_final = array();
                    foreach($subtasks as $subtask_index => $subtask) { 
                        if($subtask->parent_task_id == $task->id) {  
                            $subtasks_final[] = $subtask; 
                            unset($subtasks[$subtask_index]); 
                        }
                    }
                    $tasks->subtasks = $subtasks_final; // somehow enter here recursive loop? 

        }   



    return $tasks;


}
function buildTree(array $elements, $parentId = 0) {
$branch = array();

foreach ($elements as $element) {
    if ($element['parent_id'] == $parentId) {
        $children = buildTree($elements, $element['id']);
        if ($children) {
            $element['children'] = $children;
        }
        $branch[] = $element;
    }
}

return $branch;
}

$tree = buildTree($rows);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM