Home >Backend Development >PHP Tutorial >求个递归算法解决方法

求个递归算法解决方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-13 11:51:00852browse

求个递归算法
id    name    pid
1    AAA        0
2    BBB       1
3    CCC      2
4    DDD      2
5    EEE       0
6    FFF        5
7    GGG     6

得出

AAA
    BBB
        CCC => 'CCC'
        DDD => 'DDD'
EEE
    FFF
        GGG => 'GGG'
------解决方案--------------------
http://bbs.csdn.net/topics/390731721
------解决方案--------------------

function tree($pid=0) {<br />  $res = array();<br />  $rs = mysql_query("select * from tbl_name where pid=$pid");<br />  while($row = mysql_fetch_assoc($rs)) {<br />    $res[$row['name']] = ($t = tree($row['id'])) ? $t : $row['name'];<br />  }<br />  return $res;<br />}
      
------解决方案--------------------
原理是一样的,楼主把1楼链接里的代码修改下就可以满足要求了

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn