PHP递归,遍历表

时间:2014-05-19 12:03:52

标签: php iteration

我有一些包含id,父ID和名称的表。 当我遍历表时,我希望在id具有parent_id时构建一个带有名称的字符串。 例如id = 6.它有父2,id 2有父1,所以字符串应该是六/二/一

+---+---------+-------+
|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 |
+---+---------+-------+

以下是我的代码。它是symfony2代码

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;
}

2 个答案:

答案 0 :(得分:2)

从数据库中将它们全部放在数组中

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

输出:

六/二/一

<强> Demo

答案 1 :(得分:1)

首先,您可以将数据存储在数组中,然后您可以尝试使用它,它应该可以工作(代码测试):

<?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/

?>