×oPHPÕaô³¤Ê±¼a£¬¢ÏÖºǫ¹ÜÀiϵͳ²»¿ÉÉÙµÄÒ»¸oÓ¦ÓÃÄ£¿e¾ÍÊǶÔÀ¸Ä¿µÄÖÀa£¬Ò»°aÇe¿oÏÂÀ¸Ä¿¶¼Òª×o³ÉÊÇÎÞÏÞ¼¶µÄ£¬Ò²¾ÍÊÇ˵ÿ¸oÀ¸Ä¿ÀiÂÛÉ϶¼¿ÉÒÔÌi¼Ó×ÓÀ¸Ä¿¡£ÔÚÎÒ¿´À´ÕaÖÖÇe¿o´¦ÀiÆðÀ´ÕuÌaÉÏ˵Ҳ²»ÊǺܸ´ÔÓ£¬Î¨Ò»Ò»¸oÏa¶ÔÀ´Ëµ½ÏÄѵĵaÊÇÎÞÏÞ¼¶À¸Ä¿µÄ²eѯ¡£
ÏÂÃae¾ÍÕaÖÖÇe¿oÎÒÀ´Ïo´o¼Ò×oÒ»¸o¼oµ¥µÄ½eÉÜ£¬¶ÔÓÚÕaÖÖÎÞÏÞ¼¶À¸Ä¿µÄ²eѯһ°aÇe¿oÏÂÓÐÁ½Öֽʽ£¬ÆaÖÐÒ»ÖÖ¾ÍÊÇʹÓÃÕ»µÄ»uÖÆ£¬ÁiÒ»ÖÖÊÇʹÓõݹeº¯ÊýµÄ½Ê½£¨µ±È»µÝ¹eº¯ÊýʵÏÖ»uÖÆÒ²ÊǽeÖuÓÚÕ»À´ÊµÏֵģ(C)¡£¾ÍÕaÁ½ÖֽʽÏÂÃaeÎÒÃÇÖ±ð½eÉÜ¡£
*µÝ¹eº¯ÊýʵÏֽʽ**
ÉÏÃaeÌaµ½£¬µÝ¹eº¯ÊýµÄÒ²ÊǽeÖuÓÚÕ»µÄ»uÖÆʵÏֵģ¬µ«Êǵײa¶ÔÓÚÕ»µÄ´¦Ài¶ÔÓÚ³ÌÐoÔ±À´Ëµ¶¼ÊÇ͸Ã÷µÄ£¬³ÌÐoÔ±Ö»ÐeÒª¹ØÐÄÓ¦ÓõÄʵÏÖÂß¼¡£ËuÒÔ˵ʹÓõݹe´¦ÀiÉÏÊoÎÊÌaÀi½aÆðÀ´±È½ÏÈÝÒ×£¬´uÂeÒ²±È½Ï¼o½a¡£
¼ÈȻʹÓõݹeº¯Êý£¬¿´Ãu×ÖÎÒÃǾÍÖªµÀ±ØÐe½eÖuÓÚ×Ô¶¨ÒaµÄº¯Êý¡£ÎÒÏÈ´o¸Å˵һÏÂÆaʵÏÖ˼£¬¾ßÌaϸ½ÚÎÒÃÇ´Ó³ÔÚ´uÂeÖС£
¶ÔÓÚÿһ²aµÄº¯ÊýÆaÖ÷Òª×oµÄ¹¤×÷¾ÍÊDzeÕÒ¸¸IdΪµ±Ç°IdµÄÀ¸Ä¿£¬²eÕÒµ½ÒÔºoÔٴε÷ÓÃ×ÔÉiº¯Êý£¬½«²eÕÒµ½µÄÀ¸Ä¿µÄid×÷ΪÏÂÒ»²aµÄ¸¸id¡£
ÆaÁ÷³ÌͼÈçÏÂ
ͼһ
²»ÖªµÀ¶ÔÓÚÉÏÃaeµÄ½aÊÍ´o¼ÒÄܲ»ÄÜÀi½a£¬Ã»¹ØϵÎÒÃÇÏÂÃaeÖ±½Ó¿´´uÂe
<?php
/**
* ¸oÈ˲(C)¿Í£º¼£Òa²(C)¿Í
* ²(C)¿ÍµØÖ*£ºwww.onmpw.com
* µÝ¹eʵÏÖÎÞÏÞ¼«*ÖÀa
*/
$channels = array(
array('id'=>1,'name'=>"ÒÂ*þ",'parId'=>0),
array('id'=>2,'name'=>"Êe¼®",'parId'=>0),
array('id'=>3,'name'=>"TÐo",'parId'=>1),
array('id'=>4,'name'=>"¿a×Ó",'parId'=>1),
array('id'=>5,'name'=>"Ь×Ó",'parId'=>1),
array('id'=>6,'name'=>"ƤЬ",'parId'=>5),
array('id'=>7,'name'=>"Ô˶¯Ð¬",'parId'=>5),
array('id'=>8,'name'=>"ÄÍ¿Ë",'parId'=>7),
array('id'=>9,'name'=>"ÄÍ¿Ë",'parId'=>3),
array('id'=>10,'name'=>"ºeÐǶu¿Ë",'parId'=>7),
array('id'=>11,'name'=>"С˵",'parId'=>2),
array('id'=>12,'name'=>"¿Æ»ÃС˵",'parId'=>11),
array('id'=>13,'name'=>"¹ÅµaÃuÖø",'parId'=>11),
array('id'=>14,'name'=>"ÎÄѧ",'parId'=>2),
array('id'=>15,'name'=>"ËÄÊeÎa¾",'parId'=>14)
);
$html = array();
/**
* µÝ¹e²eÕÒ¸¸idΪ$paridµÄ½aµa
* @param array $html °´ÕÕ¸¸-¡*×ӵĽa¹¹´ae*ŲeÕÒ³oÀ´µÄ½aµa
* @param int $parid Ö¸¶¨µÄ¸¸id
* @param array $channels Êý¾ÝÊý×e
* @param int $dep ±eÀuµÄÉi¶È£¬³oʼ»¯Îª1
*/
function getChild(&$html,$parid,$channels,$dep){
/*
* ±eÀuÊý¾Ý£¬²eÕÒparIdΪ²ÎÊý$paridÖ¸¶¨µÄid
*/
for($i = 0;$i<count($channels);$i++){
if($channels[$i]['parId'] == $parid){
$html[] = array('id'=>$channels[$i]['id'],'name'=>$channels[$i]['name'],'dep'=>$dep);
getChild($html,$channels[$i]['id'],$channels,$dep+1);
}
}
}
getChild($html,0,$channels,1);
?>
ÕaÊǵݹeʵÏÖÎÞÏÞ¼¶À¸Ä¿²eѯµÄºËÐÄ´uÂe£¬½aºÏͼһ¶ÔÆaʵÏÖÁ÷³ÌÓ¦¸ÃÓÐÒ»¸o½ÏÇaÎuµÄÈÏʶ¡£
*ǵݹe£¬¼´Ê¹ÓÃÕ»»uÖÆʵÏÖÎÞÏÞ¼¶À¸Ä¿µÄ²eѯ
ÔÚÉÏÃaeÎÒÃÇ´o¸Å½eÉÜÁËÒ»ÏÂʹÓõݹeµÄ½Ê½ÊµÏÖÎÞÏÞ¼¶À¸Ä¿µÄ²eѯ£¬ÏÂÃaeÎÒÃǼoµ¥½eÉÜÒ»ÏÂǵݹeµÄ½Ê½¡£Ëa˵²»Óõݹeº¯ÊýµÄ½Ê½£¬µ«ÊǼøÓÚÎÞÏÞ¼¶À¸Ä¿µÄ½a¹¹Ò³ÐeÒª²Î¿¼µÝ¹eµÄʵÏÖ»uÖÆ¡ª¡ªÕ»µÄ»uÖÆ£¬½a¾oÕaÒ»ÎÊÌa¡£
ÔÚÉÏѧµÄʱºoÀÏʦ¾Í˵£¬ÆaʵջµÄºËÐÄ»uÖÆÒ²¾ÍËĸo×Ö£ºÏȽøºo³o¡£
ÔÚÕa¶ÔÓÚÕ»µÄ»uÖƲ»¶a˵£¬Ö÷Ҫ˵һÏÂÈçºÎ½eÖuջʵÏÖÎÞÏÞ¼¶À¸Ä¿²eѯ¡£
1. Ê×ÏȽ«¶¥¼¶À¸Ä¿Ñ¹ÈeÕ»ÖÐ
2. ½«Õ»¶¥ÔªËسoÕ»
3. ½«³oÕ»ÔªËØ´aeÈeÊý×eÖУ¬±e¼ÇÆaÉi¶È£¨ÆaÉi¶È¾ÍÊÇÔÚÆa¸¸À¸Ä¿µÄÉi¶ÈÉÏÃae¼Ó1£(C)
4. ÒÔ³oÕ»µÄÔªËØΪ¸¸À¸Ä¿£¬²eÕÒÆa×ÓÀ¸Ä¿
5. ½«²eÕÒµ½µÄ×ÓÀ¸Ä¿ÈeÕ»£¬Öظ´²½Öe2
6. ÅжÏջΪ¿ÕµÄ»°£¬Á÷³Ì½aÊø;
ͨ¹ý¶ÔÒÔÉϲ½ÖeµÄÒe£¬¿ÉÒÔ½«ÕaÐ(C)²½ÖeÒe³ÉPHP´uÂe£¬ÆaºËÐÄ´uÂeÈçÏÂ
<?php
/**
* ¸oÈ˲(C)¿Í£º¼£Òa²(C)¿Í
* ²(C)¿ÍµØÖ*£ºwww.onmpw.com
*ʹÓÃ*ǵݹe£¬¼´Ê¹ÓÃÕ»µÄ*½Ê½ÊµÏÖÀ¸Ä¿µÄÎÞÏÞ¼«*ÖÀa²eѯ
*/
$channels = array(
array('id'=>1,'name'=>"ÒÂ*þ",'parId'=>0),
array('id'=>2,'name'=>"Êe¼®",'parId'=>0),
array('id'=>3,'name'=>"TÐo",'parId'=>1),
array('id'=>4,'name'=>"¿a×Ó",'parId'=>1),
array('id'=>5,'name'=>"Ь×Ó",'parId'=>1),
array('id'=>6,'name'=>"ƤЬ",'parId'=>5),
array('id'=>7,'name'=>"Ô˶¯Ð¬",'parId'=>5),
array('id'=>8,'name'=>"ÄÍ¿Ë",'parId'=>7),
array('id'=>9,'name'=>"ÄÍ¿Ë",'parId'=>3),
array('id'=>10,'name'=>"ºeÐǶu¿Ë",'parId'=>7),
array('id'=>11,'name'=>"С˵",'parId'=>2),
array('id'=>12,'name'=>"¿Æ»ÃС˵",'parId'=>11),
array('id'=>13,'name'=>"¹ÅµaÃuÖø",'parId'=>11),
array('id'=>14,'name'=>"ÎÄѧ",'parId'=>2),
array('id'=>15,'name'=>"ËÄÊeÎa¾",'parId'=>14)
);
$stack = array(); //¶¨ÒaÒ»¸o¿ÕÕ»
$html = array(); //ÓÃÀ´±£´ae¸÷¸oÀ¸Ä¿Ö®¼aµÄ¹ØϵÒÔ¼°¸ÃÀ¸Ä¿µÄÉi¶È
/*
* ×Ô¶¨ÒaÈeÕ»º¯Êý
*/
function pushStack(&$stack,$channel,$dep){
array_push($stack, array('channel'=>$channel,'dep'=>$dep));
}
/*
* ×Ô¶¨Òa³oÕ»º¯Êý
*/
function popStack(&$stack){
return array_pop($stack);
}
/*
* Ê×ÏȽ«¶¥¼¶À¸Ä¿Ñ¹ÈeÕ»ÖÐ
*/
foreach($channels as $key=>$val){
if($val['parId'] == 0)
pushStack($stack,$val,0);
}
/*
* ½«Õ»ÖеÄÔªËسoÕ»£¬²eÕÒÆa×ÓÀ¸Ä¿
*/
do{
$par = popStack($stack); //½«Õ»¶¥ÔªËسoÕ»
/*
* ²eÕÒÒÔ´ËÀ¸Ä¿Îª¸¸¼¶À¸Ä¿µÄid£¬½«ÕaÐ(C)À¸Ä¿ÈeÕ»
*/
for($i=0;$i<count($channels);$i++){
if($channels[$i]['parId'] == $par['channel']['id']){
pushStack($stack,$channels[$i],$par['dep']+1);
}
}
/*
* ½«³oÕ»µÄÀ¸Ä¿ÒÔ¼°¸ÃÀ¸Ä¿µÄÉi¶È±£´aeµ½Êý×eÖÐ
*/
$html[] = array('id'=>$par['channel']['id'],'name'=>$par['channel']['name'],'dep'=>$par['dep']);
}while(count($stack)>0);
ÉÏÃae¾ÍÊÇʹÓÃǵݹe½Ê½ÊµÏֵġ£
ÏÂÔØ´uÂe£ºhttps://github.com/onmpw/phpApp
×ܽa
ÉÏÃaeÁ½Öֽʽ¸÷ÓÐÀu±×£¬ËaȻʵÏÖÐÎʽÉÏÃae²»Í¬£¬µ«ÊǼøÓÚÎÞÏÞ¼¶À¸Ä¿µÄ½a¹¹£¬¶þÕßʵÏֵĻuÖƶ¼ÊÇÏaͬµÄ¡ª¡ª¶¼½eÖuÕ»µÄ½Ê½À´ÊµÏÖ¡£ÔÚÏÖʵÇe¿oÖУ¬ÎÒÃÇÒª¸u¾ÝÏÖʵÇe¿oµÄÐeҪѡÔñÒ»ÖÖ*½Ê½À´ÊµÏÖ¡£