以前从来没写过递归(其实想想,对算法完全没概念),刚好有这个需求,试着写了一下,发现也挺容易的,特别记录一下。
数据库是dedecms默认的,dede_arctype是保存栏目的表,reid是栏目的父级栏目id。
<span><span>$array</span> = <span>array</span><span>(); get_sons(</span><span>$type</span>, <span>$array</span><span>); </span><span>var_dump</span>(<span>$array</span><span>); </span><span>function</span> get_sons(<span>$type</span>, &<span>$current_array</span><span>){ </span><span>$result</span> = <span>mysql_query</span>("select id from dede_arctype where reid = {<span>$type</span>}"<span>); </span><span>while</span>(<span>$row</span> = <span>mysql_fetch_assoc</span>(<span>$result</span><span>)){ </span><span>$current_array</span>[] = <span>$row</span>['id'<span>]; get_sons(</span><span>$row</span>['id'], <span>$current_array</span><span>); } }</span></span>