찾다

PHP生成树

May 25, 2016 pm 05:07 PM

php生成一个树,可以用于产品分类 

1. [图片] QQ截图20130225204020.jpg    

PHP生成树

2. [代码][PHP]代码  

<?php
class Tree
{
    public $data=array();
    public $cateArray=array();
	public $res=array();
    function Tree()
    {

    }
    function setNode ($id, $parent, $value)
    {
	    $parent = $parent?$parent:0;
	    $this->data[$id] = $value;
	    //print_r($this->data);
	    //echo "\r\n";
	    $this->cateArray[$id] = $parent;		//节点数组
	    //print_r($this->cateArray);
    }
    function getChildsTree($id=0)
    {
	    $childs=array();
	    foreach ($this->cateArray as $child=>$parent)
	    {
		    if ($parent==$id)
		    {
		    	$childs[$child]=$this->getChildsTree($child);
		    }
	    }
	    print_r($childs)."/r/n";
	    return $childs;
    }
    function getParentsTree($id=0)
    {
	    $parents=array();
	    foreach ($this->cateArray as $child=>$parent)
	    {
		    if ($child ==$id)
		    {
		    	$parents[$parent]=$this->getParentsTree($parent);
		    }
	    }
	    print_r($parents)."/r/n";
	    return $parents;
    }
    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;
    }
    
    function getParents($id)
    {
	    $parentArray=array();
	    $parents=$this->getParent($id);
	    foreach ($parents as $parent)
	    {
		    $parentArray[]=$parent;
		    $parentArray=array_merge($parentArray,$this->getParents($parent));
	    }
	    return $parentArray;
    }
    
    function getParent($id)
    {
	    $parents=array();
	    foreach ($this->cateArray as $child=>$parent)
	    {
	    if ($child==$id)
	    {
	    	$parents[$parent]=$parent;
	    }
	    }
	    return $parents;
    }
    //单线获取父节点
    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=&#39;|-&#39;)
    {
    	return str_repeat($preStr,count($this->getNodeLever($id)));
    }
    function getValue ($id)
    {
    	return $this->data[$id];
    } // end func
    //获取所有节点数据生成树
    function getAll($id=0,$str="|-"){
    	if($id!=0){
    		$str=$str."|-";
    	}
    	//遍历所有数组检查parent是否有id
    	foreach($this->cateArray as $child=>$parent){
    		//检查是否有相等的ID
    		if($parent==$id){
    			$this->res[$child]=$str.$this->getValue($child);
    			$this->getAll($child,$str);
    		}
    		//$this->res[$child]=$child.$str.$this->getValue($child);
    	}
    }
}

//构造树
//setNode ($id, $parent, $value)
$tree = new Tree();
$tree->setNode("1","","电器");
$tree->setNode("2","","服装");
$tree->setNode("3","1","电脑");
$tree->setNode("4","1","家电");
$tree->setNode("5","2","男装");
$tree->setNode("6","2","女装");
$tree->setNode("7","3","笔记本");
$tree->setNode("8","3","台式机");
$tree->setNode("9","7","惠普");
$tree->setNode("10","7","戴尔");
$tree->setNode("11","8","火星人");
$tree->setNode("12","5","西装");
$tree->setNode("13","6","上衣");
$tree->setNode("14","9","惠普-N90");
$tree->setNode("15","9","惠普-N91");
$tree->setNode("16","10","戴尔A11");
$tree->setNode("17","10","戴尔A12");
$tree->setNode("18","10","戴尔A13");
$tree->setNode("19","6","裤子 ");
$tree->setNode("20","13","长袖");
$tree->setNode("21","13","短袖");
$tree->setNode("22","20","NIKE长袖");
$tree->setNode("23","20","361长袖");
$tree->setNode("24","22","NIKE长袖-均码");
$tree->setNode("25","22","NIKE长袖-短码");
$tree->setNode("26","14","惠普-N90-14寸");
$tree->setNode("27","14","惠普-N90-15寸");
$tree->setNode("28","14","惠普-N90-17寸");
$tree->setNode("29","28","惠普-N90-17寸-高性能企业版");
$tree->setNode("30","28","惠普-N90-17寸-普通家用版");
//获取一个节点的所有父节点
//print_r ($tree->getNodeLever(12));

//print_r ($tree->getParentsTree(12));

//$childs = $tree->getChildsTree(1);
//print_r($childs);
//echo "/r/n/r/n/r/n/r/n";
/*
foreach($childs as $key=>$value){
	echo $key."<br>";
	//echo $tree->getLayer($key).$tree->getValue($key)."<br>";
}
*/
$tree->getAll();
foreach($tree->res as $val){
	echo $val."<br>";
}
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>生成Tree</title>
</head>
<body>
<h5>生成标签测试</5>
<select>
<?php
	foreach($tree->res as $key=>$val){
		echo "<option value=&#39;{$key}&#39;>{$val}</option>";
	}
?>
</select>
</body>
</html>

                   

                   

성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.

핫 AI 도구

Undresser.AI Undress

Undresser.AI Undress

사실적인 누드 사진을 만들기 위한 AI 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

AI Hentai Generator

AI Hentai Generator

AI Hentai를 무료로 생성하십시오.

인기 기사

R.E.P.O. 에너지 결정과 그들이하는 일 (노란색 크리스탈)
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 최고의 그래픽 설정
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. 아무도들을 수없는 경우 오디오를 수정하는 방법
3 몇 주 전By尊渡假赌尊渡假赌尊渡假赌
WWE 2K25 : Myrise에서 모든 것을 잠금 해제하는 방법
4 몇 주 전By尊渡假赌尊渡假赌尊渡假赌

뜨거운 도구

DVWA

DVWA

DVWA(Damn Vulnerable Web App)는 매우 취약한 PHP/MySQL 웹 애플리케이션입니다. 주요 목표는 보안 전문가가 법적 환경에서 자신의 기술과 도구를 테스트하고, 웹 개발자가 웹 응용 프로그램 보안 프로세스를 더 잘 이해할 수 있도록 돕고, 교사/학생이 교실 환경 웹 응용 프로그램에서 가르치고 배울 수 있도록 돕는 것입니다. 보안. DVWA의 목표는 다양한 난이도의 간단하고 간단한 인터페이스를 통해 가장 일반적인 웹 취약점 중 일부를 연습하는 것입니다. 이 소프트웨어는

Atom Editor Mac 버전 다운로드

Atom Editor Mac 버전 다운로드

가장 인기 있는 오픈 소스 편집기

SublimeText3 Mac 버전

SublimeText3 Mac 버전

신 수준의 코드 편집 소프트웨어(SublimeText3)

SublimeText3 영어 버전

SublimeText3 영어 버전

권장 사항: Win 버전, 코드 프롬프트 지원!

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경