- 论坛徽章:
- 0
|
- <?php
- //Test Array
- $input = $input=array("0"=>Array("id"=>1,"pid"=>0,"child"=>Array("0"=>Array("id"=>7,"pid"=>1,"child"=>Array("0"=>Array("id"=>9,"pid"=>7,"child"=>Array("0"=>Array("id"=>10,"pid"=>9,"child"=>Array()))))),"1"=>Array("id"=>6,"pid"=>1,"child"=>Array("0"=>Array("id"=>8,"pid"=>6,"child"=>Array()))))),"1"=>Array("id"=>2,"pid"=>0,"child"=>Array("0"=>Array("id"=>3,"pid"=>2,"child"=>Array("0"=>Array("id"=>5,"pid"=>3,"child"=>Array()))))),"2"=>Array("id"=>4,"pid"=>0,"child"=>Array()));
- class test{
- var $target = array();
- var $code;
- function getArrayById( $searchID, $inputArray, $d){
- $d++; if($d>100){ die("D Error!");exit();}
- for($i=0; $i<sizeof($inputArray); $i++){
- if( $inputArray[$i]["id"] == $searchID ){
- //printf("Haha I gound %s!<br>", $searchID);
- //print_r($inputArray[$i]);
- array_push( $this->target, $inputArray[$i] ) ;
- }
- else{
- if(sizeof($inputArray[$i]["child"])>0){
- $this->getArrayById( $searchID, $inputArray[$i]["child"], $d);
- }
- }
- }
- }
- function getArrayInTree( $treeArray, $rn ){
- $rn++;if($rn>20){ die("Error!"); exit();}
- for($i=0; $i<sizeof($treeArray); $i++){
- if(sizeof($treeArray[$i]["child"])>0){
- $string .= "<ul><li>ID:".$treeArray[$i]["id"];
- $string .=$this->getArrayInTree($treeArray[$i]["child"], $rn);
- $string .= "</li></ul>";
- }
- else{
- $string = "<ul><li>ID:";
- $string.= $treeArray[$i]["id"];
- $string.= "</li></ul>";
- }
- }
- $rn--;
- return $string;
- }
- }
- $mytest = new test();
- $mytest->getArrayById (1, $input, 0);
- $html = $mytest->getArrayInTree( $mytest->target , 0 );
- print($html);
- ?>
复制代码
顺下来的就是如何把 ul li 美化成 属形结构
[ 本帖最后由 imbiss 于 2006-3-9 23:11 编辑 ] |
|