日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

简单好用的PHP无限分类

發(fā)布時間:2025/7/14 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 简单好用的PHP无限分类 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

/**
* author: askie
* blog: http://www.pkphp.com
* 版權(quán): 隨便用
* 無限分類
*/
class Tree
{
??????? public $data=array();
??????? public $cateArray=array();
???????
??????? function Tree()
??????? {
??????????????
??????? }
??????? function setNode ($id, $parent, $value)
??? {
??????? $parent = $parent?$parent:0;
??????? $this->data[$id]??????????????? = $value;
??????? $this->cateArray[$id]??????? = $parent;
??? }
??? function getChildsTree($id=0)
??? {
??????????? $childs=array();
??????????? foreach ($this->cateArray as $child=>$parent)
??????????? {
??????????????????? if ($parent==$id)
??????????????????? {
??????????????????????????? $childs[$child]=$this->getChildsTree($child);
??????????????????? }
???????????????????
??????????? }
??????????? return $childs;
??? }
??? function getChilds($id=0)
??? {
??????????? $childArray=array();
??????????? $childs=$this->getChild($id);
??????????? foreach ($childs as $child)
??????????? {
??????????????????? $childArray[]=$child;
??????????????????? $childArray=array_merge($childArray,$this->getChilds($child));
??????????? }
??????????? return $childArray;
??? }
??? function getChild($id)
??? {
??????????? $childs=array();
??????????? foreach ($this->cateArray as $child=>$parent)
??????????? {
??????????????????? if ($parent==$id)
??????????????????? {
??????????????????????????? $childs[$child]=$child;
??????????????????? }
??????????? }
??????????? return $childs;
??? }
??? //單線獲取父節(jié)點
??? function getNodeLever($id)
??? {
??????????? $parents=array();
??????????? if (key_exists($this->cateArray[$id],$this->cateArray))
??????????? {
??????????????????? $parents[]=$this->cateArray[$id];
??????????????????? $parents=array_merge($parents,$this->getNodeLever($this->cateArray[$id]));
??????????? }
??????????? return $parents;
??? }
??? function getLayer($id,$preStr='|-')
??? {
??????????? return str_repeat($preStr,count($this->getNodeLever($id)));
??? }
??? function getValue ($id)
??? {
??????? return $this->data[$id];
??? } // end func
}

/*$Tree = new Tree("請選擇分類");
//setNode(目錄ID,上級ID,目錄名字);
$Tree->setNode(1, 0, '目錄1');
$Tree->setNode(2, 1, '目錄2');
$Tree->setNode(5, 3, '目錄5');
$Tree->setNode(3, 0, '目錄3');
$Tree->setNode(4, 2, '目錄4');
$Tree->setNode(9, 4, '目錄9');
$Tree->setNode(6, 2, '目錄6');
$Tree->setNode(7, 2, '目錄7');
$Tree->setNode(8, 3, '目錄8');

//print_r($Tree->getChildsTree(0));
//print_r($Tree->getChild(0));
//print_r($Tree->getLayer(2));

$category = $Tree->getChilds();

//遍歷輸出
foreach ($category as $key=>$id)
{
??? echo $id.$Tree->getLayer($id, '|-').$Tree->getValue($id)."\n";
}*/

?>

轉(zhuǎn)載于:https://www.cnblogs.com/8user/archive/2008/08/24/1275295.html

總結(jié)

以上是生活随笔為你收集整理的简单好用的PHP无限分类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。