首頁  >  文章  >  php教程  >  PHP无限极分类之数组

PHP无限极分类之数组

WBOY
WBOY原創
2016-06-13 08:49:531308瀏覽

PHP无限极分类之数组

<?php
$data = array( 1 => array(&#39;id&#39; => 1,&#39;name&#39; => &#39;name1&#39;,&#39;pid&#39; => 0),
            2 => array(&#39;id&#39; => 2,&#39;name&#39; => &#39;name2&#39;,&#39;pid&#39; => 0),
            3 => array(&#39;id&#39; => 3,&#39;name&#39; => &#39;name3&#39;,&#39;pid&#39; => 1),
            4 => array(&#39;id&#39; => 4,&#39;name&#39; => &#39;name4&#39;,&#39;pid&#39; => 0),
            5 => array(&#39;id&#39; => 5,&#39;name&#39; => &#39;name5&#39;,&#39;pid&#39; => 3),
            6 => array(&#39;id&#39; => 6,&#39;name&#39; => &#39;name6&#39;,&#39;pid&#39; => 3),
    );
 $str = getTree(0);
$str = "<select name=&#39;term&#39;>" . $str . "</select>";
echo $str;
function getTree($id,$spac=0)
{
    $data = array( 1 => array(&#39;id&#39; => 1,&#39;name&#39; => &#39;name1&#39;,&#39;pid&#39; => 0),
            2 => array(&#39;id&#39; => 2,&#39;name&#39; => &#39;name2&#39;,&#39;pid&#39; => 0),
            3 => array(&#39;id&#39; => 3,&#39;name&#39; => &#39;name3&#39;,&#39;pid&#39; => 1),
            4 => array(&#39;id&#39; => 4,&#39;name&#39; => &#39;name4&#39;,&#39;pid&#39; => 0),
            5 => array(&#39;id&#39; => 5,&#39;name&#39; => &#39;name5&#39;,&#39;pid&#39; => 3),
            6 => array(&#39;id&#39; => 6,&#39;name&#39; => &#39;name6&#39;,&#39;pid&#39; => 3),
    );
    $spac = $spac + 2;
    static $str;
     
    foreach ($data as $k => $v) {
        if ($v[&#39;pid&#39;] == $id) {
            $arr[] = $v;
            $str .= "<option value=&#39;" . $v[&#39;id&#39;] . "&#39;>" . str_repeat(" ",$spac) . "|--" . $v[&#39;name&#39;] . "</option>";
            getTree($v[&#39;id&#39;],$spac);
 
        }
 
    }
     
    return $str;
}
 
 
 
?>

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn