首頁 > php教程 > php手册 > 主體

PHP无限极分类之数组

WBOY
發布: 2016-06-13 08:49:53
原創
1307 人瀏覽過

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;
}
 
 
 
?>
登入後複製

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!