程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> PHP無限極分類之數組

PHP無限極分類之數組

編輯:關於PHP編程

PHP無限極分類之數組


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved