简体   繁体   中英

PHP recursion, iterating through table

I have some table with ids, parent ids, and names. When I iterating through table I would like to build one string with names when id has parent_id. For example id = 6. It has parent 2, and id 2 has parent 1, so string should be six / two / one

+---+---------+-------+
|id |parent_id| name  |
+---------------------+
|1  | null    | one   |
|2  | 1       | two   |
|3  | null    | three |
|4  | 3       | four  |
|5  | 4       | five  |
|6  | 2       | six   |
|7  | 3       | seven |
|8  | 2       | eight |
+---+---------+-------+

Below is my code. It is symfony2 code

public function getParentTaskTree($id){
    $i = 10;
    $parentTaskTree = '';
    $taskRepository = $this->getEntityManager()
        ->getRepository('FrontendBundle:Task');
    $task = $taskRepository->findOneBy(array('id' => $id));
    if($task){
        $parentTaskTree .= $task->getParent();
        $parentTaskTree .= ' / ';
        $this->getParentTaskTree($taskRepository->findOneBy(array('parent' => $task->getParent())));
    }


    return $parentTaskTree;
}

put them all in array like this from your database

   $arr_input  = array();

$arr_input[1] = array("parent_id"=>"null", "name"=>"one");
$arr_input[2] = array("parent_id"=>"1", "name"=>"two");
$arr_input[3] = array("parent_id"=>"null", "name"=>"three");
$arr_input[4] = array("parent_id"=>"3", "name"=>"four");
$arr_input[5] = array("parent_id"=>"4", "name"=>"five");
$arr_input[6] = array("parent_id"=>"2", "name"=>"six");
$arr_input[7] = array("parent_id"=>"3", "name"=>"seven");
$arr_input[8] = array("parent_id"=>"2", "name"=>"eight");

$input_id = 6; // variable input

if(isset($arr_input[$input_id]))
{   
    echo $arr_input[$input_id]['name'];
    $parent_id = $arr_input[$input_id]['parent_id'];
    while($parent_id!='null')
    {   
        if(isset($arr_input[$parent_id]))
        {
            echo "/".$arr_input[$parent_id]['name'];
        }
        else
        {
            break;
        }
        $parent_id = $arr_input[$parent_id]['parent_id'];
    }
}

OUTPUT :

six / two / one

Demo

First you can store your data in an array, and then you could try this, it should work ( code tested ):

<?php 

    $array = array();
    $array[1] = array("id" => 1, "parent_id"=>"null", "name"=>"one");
    $array[2] = array("id" => 2, "parent_id"=>"1", "name"=>"two");
    $array[3] = array("id" => 3, "parent_id"=>"null", "name"=>"three");
    $array[4] = array("id" => 4, "parent_id"=>"3", "name"=>"four");
    $array[5] = array("id" => 5, "parent_id"=>"4", "name"=>"five");
    $array[6] = array("id" => 6, "parent_id"=>"2", "name"=>"six");
    $array[7] = array("id" => 7, "parent_id"=>"3", "name"=>"seven");
    $array[8] = array("id" => 8, "parent_id"=>"2", "name"=>"eight");

    function hasParent($array,$id){
        foreach($array as $key => $value){
            if (($array[$id]['parent_id'] != null) && ($value['id'] == $array[$id]['parent_id'])){
                return $array[$id]['parent_id'];
                break;
            }
        }
        return -1;
    }

    function mainRec ($array,$id,$string){
        $parentID = hasParent($array,$id);
        if ($parentID == -1){
            return $array[$id]['name'] . '/' . $string;
        }else{
            return mainRec($array,$parentID, $array[$id]['name'] . '/' . $string);
        }
    }

echo (mainRec($array,6,"")); //echo one/two/six/

?>

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