在id递归时更改级别

时间:2017-12-14 14:51:30

标签: php recursion

例如,我们现在以用户id 10登录,当我们的递归代码被执行时,我们得到一棵树,在这里:

Level  ID  P_ID
  1    12   10
  1    13   10
  1    14   9
  2    15   12
  2    16   14
  2    17   14
  3    18   17

应该是这样的:

 Level  ID  P_ID
  1    12   10
  1    13   10
  2    15   12

因为我们从当前的id看,即另一个id,所以构造逻辑是相同的。

递归代码:

    $res = $mysql->query("SELECT * FROM table"); 

$data = array();
while($row = mysqli_fetch_assoc($res)){
    $data['spids'][$row['id']]        = $row['sponsor_id'];

    if(isset($data['level'][$row['sponsor_id']])){
        $level = $data['level'][$row['sponsor_id']] + 1;
    } else {
        $level = 0;
    }

    $data['level'][$row['id']] = $level;
}

echo '
<table>
        <tr>
            <td>Level</td>
            <td>ID</td>
            <td>P_ID</td>
        </tr>';
foreach($data['level'] as $id=>$level){
    if ($level != 0) {
    echo '
    <tr>
        <td>'.$level.'</td>
        <td>'.$id.'</td>
        <td>'.$data['spids'][$id].'</td>

    </tr>';
    }
}
echo '
</table>';

任何人都可以帮助如何更改部分功能,以便树的选择正确吗?

1 个答案:

答案 0 :(得分:1)

这是您的递归过程。有关步骤的说明,请参阅内联注释。我已经添加了您的注释表数据来代替查询和结果集生成,以便我可以测试我的代码。

(您只需删除两个硬编码结果集并取消注释查询和检索行。)

代码:(Demo

function recursive_search($haystack,$needles,$level=1,&$result=[]){
    foreach($haystack as $k=>$row){
        //echo "\nchecking: {$row['P_ID']} against: ".implode(',',$needles);
        if(in_array($row['P_ID'],$needles)){  // look for qualifying rows
            //echo " found";
            $result[]=array_merge(['Level'=>$level],$row);                   // store qualifying row
            $new_needles[]=$row['ID'];        // store the search value for the next recursive call
            unset($haystack[$k]);             // reduce the haystack to improve efficiency and avoid infinite loop
        }
    }
    if(isset($new_needles)){
        recursive_search($haystack,$new_needles,$level+1,$result);  // recurse as long as there are new needles declared
    }
    return $result;
}

// $res = $mysql->query("SELECT id AS ID, sponsor_id AS P_ID, username AS Name FROM user");
// for($resultset=[]; $row=$res->fetch_assoc(); $resultset[]=$row);  // inspired by: http://php.net/manual/en/mysqli-result.fetch-assoc.php#112924
$resultset=[
    ['ID'=>'0','P_ID'=>NULL,'Name'=>'RMagen'],
    ['ID'=>'1','P_ID'=>NULL,'Name'=>'siokpol'],
    ['ID'=>'2','P_ID'=>NULL,'Name'=>'green'],
    ['ID'=>'3','P_ID'=>NULL,'Name'=>'test12'],
    ['ID'=>'5','P_ID'=>NULL,'Name'=>'Dist'],
    ['ID'=>'7','P_ID'=>NULL,'Name'=>'name'],
    ['ID'=>'8','P_ID'=>NULL,'Name'=>'sas'],
    ['ID'=>'9','P_ID'=>NULL,'Name'=>'dad'],
    ['ID'=>'10','P_ID'=>NULL,'Name'=>'name541'],
    ['ID'=>'11','P_ID'=>'1','Name'=>'ini'],
    ['ID'=>'12','P_ID'=>'9','Name'=>'dad2'],
    ['ID'=>'13','P_ID'=>'9','Name'=>'dad3'],
    ['ID'=>'14','P_ID'=>'9','Name'=>'dad4'],
    ['ID'=>'15','P_ID'=>'9','Name'=>'dad5'],
    ['ID'=>'16','P_ID'=>'9','Name'=>'dad6'],
    ['ID'=>'17','P_ID'=>'12','Name'=>'dad21'],
    ['ID'=>'18','P_ID'=>'12','Name'=>'dad22'],
    ['ID'=>'19','P_ID'=>'12','Name'=>'dad23'],
    ['ID'=>'20','P_ID'=>'12','Name'=>'dad24'],
    ['ID'=>'21','P_ID'=>'13','Name'=>'dad31'],
    ['ID'=>'22','P_ID'=>'13','Name'=>'dad32'],
    ['ID'=>'23','P_ID'=>'13','Name'=>'dad33'],
    ['ID'=>'24','P_ID'=>'14','Name'=>'dad41'],
    ['ID'=>'25','P_ID'=>'14','Name'=>'dad42'],
    ['ID'=>'26','P_ID'=>'14','Name'=>'dad43'],
    ['ID'=>'27','P_ID'=>'17','Name'=>'dad211'],
    ['ID'=>'28','P_ID'=>'17','Name'=>'dad212'],
    ['ID'=>'29','P_ID'=>'17','Name'=>'dad213'],
    ['ID'=>'30','P_ID'=>'18','Name'=>'dad221'],
    ['ID'=>'31','P_ID'=>'18','Name'=>'dad222'],
    ['ID'=>'32','P_ID'=>'18','Name'=>'dad223'],
    ['ID'=>'33','P_ID'=>'27','Name'=>'dad2111'],
    ['ID'=>'34','P_ID'=>'27','Name'=>'dad2112'],
    ['ID'=>'35','P_ID'=>'27','Name'=>'dad2113'],
    ['ID'=>'36','P_ID'=>'30','Name'=>'dad2211'],
    ['ID'=>'37','P_ID'=>'30','Name'=>'dad2212'],
    ['ID'=>'38','P_ID'=>'30','Name'=>'dad2213'],
    ['ID'=>'39','P_ID'=>NULL,'Name'=>'sas2'],
    ['ID'=>'40','P_ID'=>NULL,'Name'=>'sas3'],
    ['ID'=>'42','P_ID'=>'14','Name'=>'dad433']
];

//$idUser = 10; // YIELDS NO RECURSIVE SEARCH RESULTS
$idUser = 1;  // YIELDS ONE RECURSIVE SEARCH RESULT
//$idUser = 9;  // YIELDS MANY RECURSIVE SEARCH RESULTS
$array=recursive_search($resultset,[$idUser]);  // pass `$idUser` as an array element

echo "<table>";
    echo "<tr><td>Level</td><td>ID</td><td>P_ID</td><td>Name</td></tr>";
    foreach($array as $row){
        echo "<tr><td>{$row['Level']}</td><td>{$row['ID']}</td><td>{$row['P_ID']}</td><td>{$row['Name']}</td></tr>";
    }
echo "</table>";

输出:

<table>
    <tr>
        <td>Level</td>
        <td>ID</td>
        <td>P_ID</td>
        <td>Name</td>
    </tr>
    <tr>
        <td>1</td>
        <td>11</td>
        <td>1</td>
        <td>ini</td>
    </tr>
</table>