- 相關(guān)推薦
PHP實(shí)現(xiàn)無(wú)限級(jí)分類的方法
文章主要介紹了在不使用遞歸的情況下PHP實(shí)現(xiàn)無(wú)限級(jí)分類,感興趣的小伙伴們可以參考一下.
無(wú)限級(jí)分類在開(kāi)發(fā)中經(jīng)常使用,例如:部門(mén)結(jié)構(gòu)、文章分類。無(wú)限級(jí)分類的難點(diǎn)在于“輸出”和“查詢”,例如
將文章分類輸出為<ul>列表形式;
查找分類A下面所有分類包含的文章。
1.實(shí)現(xiàn)原理
幾種常見(jiàn)的實(shí)現(xiàn)方法,各有利弊。其中“改進(jìn)前序遍歷樹(shù)”數(shù)據(jù)結(jié)構(gòu),便于輸出和查詢,但是在移動(dòng)分類和常規(guī)理解上有些復(fù)雜。
2.數(shù)據(jù)結(jié)構(gòu)
12
<?php
$list = array(
array('id'=>1, 'fid'=>0, 'title' => '中國(guó)'),
array('id'=>2, 'fid'=>1, 'title' => '江蘇'),
array('id'=>3, 'fid'=>1, 'title' => '安徽'),
array('id'=>4, 'fid'=>8, 'title' => '江陰'),
array('id'=>5, 'fid'=>3, 'title' => '蕪湖'),
array('id'=>6, 'fid'=>3, 'title' => '合肥'),
array('id'=>7, 'fid'=>3, 'title' => '蚌埠'),
array('id'=>8, 'fid'=>8, 'title' => '無(wú)錫')
);
?>
各分類之間通過(guò)父類id(即fid)進(jìn)行級(jí)別“串聯(lián)”,形成一棵分類樹(shù)。在進(jìn)行串聯(lián)時(shí)候有一點(diǎn)值得注意:分類A的fid不可以是其子類的id。
在使用這種數(shù)據(jù)結(jié)構(gòu)進(jìn)行輸出時(shí)最常用的算法就是“遞歸”,熟悉PHP語(yǔ)言的朋友肯定知道,PHP不擅長(zhǎng)遞歸 ,而且遞歸次數(shù)有限(100次左右,因操作系統(tǒng)和配置而異)。
由于所有的遞歸均可以使用循環(huán)實(shí)現(xiàn),本文根據(jù)PHP語(yǔ)言特點(diǎn)編寫(xiě)了一套關(guān)于“無(wú)限級(jí)”分類的函數(shù),相比遞歸實(shí)現(xiàn)而言效率更高。
3.輸出ul列表形式
將上述數(shù)據(jù)輸出為下面的HTML
<ul>
<li class="first-child">
<p>江蘇</p>
<ul>
<li class="first-child last-child">
<p>無(wú)錫</p>
<ul>
<li class="first-child last-child">
<p>江陰</p>
</li>
</ul>
</li>
</ul>
</li>
<li class="last-child">
<p>安徽</p>
<ul>
<li class="first-child"><p>蕪湖</p></li>
<li><p>合肥</p></li>
<li class="last-child"><p>蚌埠</p></li>
</ul>
</li>
</ul>
這種HTML結(jié)構(gòu)在前端使用(使用JavaScript和CSS構(gòu)造可折疊樹(shù))十分方便。具體實(shí)現(xiàn)程序如下:
?
1
<ul><?php echo get_tree_ul($list, 1); ?></ul>
4.輸出option列表形式
<select>
<option value="2">江蘇</option>
<option value="8"> 無(wú)錫</option>
<option value="4"> 江陰</option>
<option value="3">安徽</option>
<option value="5"> 蕪湖</option>
<option value="6"> 合肥</option>
<option value="7"> 蚌埠</option>
</select>
具體實(shí)現(xiàn)程序如下:
9
<select>
<?php
// get_tree_option()返回?cái)?shù)組,并為每個(gè)元素增加了“深度”(即depth)列,直接輸出即可
$options = get_tree_option($list, 1);
foreach($options as $op) {
echo '<option value="' . $op['id'] .'">' . str_repeat(" ", $op['depth'] * 4) . $op['title'] . '<;/option>';
}
?>
<;/select>
5. 查找某一分類的所有子類
4
<?php
$children = get_tree_child($list, 0);
echo implode(',', $children); // 輸出:1,3,2,7,6,5,8,4
?>
6. 查找某一分類的所有父類
<?php
$children = get_tree_parent($list, 4);
echo implode(',', $children); //8, 2, 10
?>
7. 相關(guān)函數(shù)
180
<?php
function get_tree_child($data, $fid) {
$result = array();
$fids = array($fid);
do {
$cids = array();
$flag = false;
foreach($fids as $fid) {
for($i = count($data) - 1; $i >=0 ; $i--) {
$node = $data[$i];
if($node['fid'] == $fid) {
array_splice($data, $i , 1);
$result[] = $node['id'];
$cids[] = $node['id'];
$flag = true;
}
}
}
$fids = $cids;
} while($flag === true);
return $result;
}
function get_tree_parent($data, $id) {
$result = array();
$obj = array();
foreach($data as $node) {
$obj[$node['id']] = $node;
}
$value = isset($obj[$id]) ? $obj[$id] : null;
while($value) {
$id = null;
foreach($data as $node) {
if($node['id'] == $value['fid']) {
$id = $node['id'];
$result[] = $node['id'];
break;
}
}
if($id === null) {
$result[] = $value['fid'];
}
$value = isset($obj[$id]) ? $obj[$id] : null;
}
unset($obj);
return $result;
}
function get_tree_ul($data, $fid) {
$stack = array($fid);
$child = array();
$added_left = array();
$added_right= array();
$html_left = array();
$html_right = array();
$obj = array();
$loop = 0;
foreach($data as $node) {
$pid = $node['fid'];
if(!isset($child[$pid])) {
$child[$pid] = array();
}
array_push($child[$pid], $node['id']);
$obj[$node['id']] = $node;
}
while (count($stack) > 0) {
$id = $stack[0];
$flag = false;
$node = isset($obj[$id]) ? $obj[$id] : null;
if (isset($child[$id])) {
$cids = $child[$id];
$length = count($cids);
for($i = $length - 1; $i >= 0; $i--) {
array_unshift($stack, $cids[$i]);
}
$obj[$cids[$length - 1]]['isLastChild'] = true;
$obj[$cids[0]]['isFirstChild'] = true;
$flag = true;
}
if ($id != $fid && $node && !isset($added_left[$id])) {
if(isset($node['isFirstChild']) && isset($node['isLastChild'])) {
$html_left[] = '<li class="first-child last-child">';
} else if(isset($node['isFirstChild'])) {
$html_left[] = '<li class="first-child">';
} else if(isset($node['isLastChild'])) {
$html_left[] = '<li class="last-child">';
} else {
$html_left[] = '<li>';
}
$html_left[] = ($flag === true) ? "<p>{$node['title']}</p><ul>" : "<p>{$node['title']}</p>";
$added_left[$id] = true;
}
if ($id != $fid && $node && !isset($added_right[$id])) {
$html_right[] = ($flag === true) ? '</ul></li>' : '</li>';
$added_right[$id] = true;
}
if ($flag == false) {
if($node) {
$cids = $child[$node['fid']];
for ($i = count($cids) - 1; $i >= 0; $i--) {
if ($cids[$i] == $id) {
array_splice($child[$node['fid']], $i, 1);
break;
}
}
if(count($child[$node['fid']]) == 0) {
$child[$node['fid']] = null;
}
}
array_push($html_left, array_pop($html_right));
array_shift($stack);
}
$loop++;
if($loop > 5000) return $html_left;
}
unset($child);
unset($obj);
return implode('', $html_left);
}
function get_tree_option($data, $fid) {
$stack = array($fid);
$child = array();
$added = array();
$options = array();
$obj = array();
$loop = 0;
$depth = -1;
foreach($data as $node) {
$pid = $node['fid'];
if(!isset($child[$pid])) {
$child[$pid] = array();
}
array_push($child[$pid], $node['id']);
$obj[$node['id']] = $node;
}
while (count($stack) > 0) {
$id = $stack[0];
$flag = false;
$node = isset($obj[$id]) ? $obj[$id] : null;
if (isset($child[$id])) {
for($i = count($child[$id]) - 1; $i >= 0; $i--) {
array_unshift($stack, $child[$id][$i]);
}
$flag = true;
}
if ($id != $fid && $node && !isset($added[$id])) {
$node['depth'] = $depth;
$options[] = $node;
$added[$id] = true;
}
if($flag == true){
$depth++;
} else {
if($node) {
for ($i = count($child[$node['fid']]) - 1; $i >= 0; $i--) {
if ($child[$node['fid']][$i] == $id) {
array_splice($child[$node['fid']], $i, 1);
break;
}
}
if(count($child[$node['fid']]) == 0) {
$child[$node['fid']] = null;
$depth--;
}
}
array_shift($stack);
}
$loop++;
if($loop > 5000) return $options;
}
unset($child);
unset($obj);
return $options;
}
?>
【PHP實(shí)現(xiàn)無(wú)限級(jí)分類的方法】相關(guān)文章:
php實(shí)現(xiàn)無(wú)限級(jí)分類實(shí)現(xiàn)代碼07-03
php無(wú)限分類方法講解04-14
php+mysql實(shí)現(xiàn)無(wú)限分類實(shí)例詳解07-23