前言:之前实现无限极分类,都是自己写,也没管效率和其他人的实现方式,今天特此总结下各种无限极分类的实现方式。
一、从开源网站上看到的,非常非常的简介。一次性查出所有数据,然后再分类(传引用是核心)
$items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => '江西省'), 2 => array('id' => 2, 'pid' => 0, 'name' => '黑龙江省'), 3 => array('id' => 3, 'pid' => 1, 'name' => '南昌市'), 4 => array('id' => 4, 'pid' => 2, 'name' => '哈尔滨市'), 5 => array('id' => 5, 'pid' => 2, 'name' => '鸡西市'), 6 => array('id' => 6, 'pid' => 4, 'name' => '香坊区'), 7 => array('id' => 7, 'pid' => 4, 'name' => '南岗区'), 8 => array('id' => 8, 'pid' => 6, 'name' => '和兴路'), 9 => array('id' => 9, 'pid' => 7, 'name' => '西大直街'), 10 => array('id' => 10, 'pid' => 8, 'name' => '东北林业大学'), 11 => array('id' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'), 12 => array('id' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'), 13 => array('id' => 13, 'pid' => 1, 'name' => '赣州市'), 14 => array('id' => 14, 'pid' => 13, 'name' => '赣县'), 15 => array('id' => 15, 'pid' => 13, 'name' => '于都县'), 16 => array('id' => 16, 'pid' => 14, 'name' => '茅店镇'), 17 => array('id' => 17, 'pid' => 14, 'name' => '大田乡'), 18 => array('id' => 18, 'pid' => 16, 'name' => '义源村'), 19 => array('id' => 19, 'pid' => 16, 'name' => '上坝村'), ); /** * 将数据格式化成树形结构 * @author Xuefen.Tong * @param array $items * @return array */ function genTree9($items) { $tree = array(); //格式化好的树 foreach ($items as $item) if (isset($items[$item['pid']])) $items[$item['pid']]['son'][] = &$items[$item['id']]; else $tree[] = &$items[$item['id']]; return $tree; } echo ""; print_r(genTree5($items)); // 结果 Array ( [0] => Array ( [id] => 1 [pid] => 0 [name] => 江西省 [son] => Array ( [0] => Array ( [id] => 3 [pid] => 1 [name] => 南昌市 ) [1] => Array ( [id] => 13 [pid] => 1 [name] => 赣州市 [son] => Array ( [0] => Array ( [id] => 14 [pid] => 13 [name] => 赣县 [son] => Array ( [0] => Array ( [id] => 16 [pid] => 14 [name] => 茅店镇 [son] => Array ( [0] => Array ( [id] => 18 [pid] => 16 [name] => 义源村 ) [1] => Array ( [id] => 19 [pid] => 16 [name] => 上坝村 ) ) ) [1] => Array ( [id] => 17 [pid] => 14 [name] => 大田乡 ) ) ) [1] => Array ( [id] => 15 [pid] => 13 [name] => 于都县 ) ) ) ) ) [1] => Array ( [id] => 2 [pid] => 0 [name] => 黑龙江省 [son] => Array ( [0] => Array ( [id] => 4 [pid] => 2 [name] => 哈尔滨市 [son] => Array ( [0] => Array ( [id] => 6 [pid] => 4 [name] => 香坊区 [son] => Array ( [0] => Array ( [id] => 8 [pid] => 6 [name] => 和兴路 [son] => Array ( [0] => Array ( [id] => 10 [pid] => 8 [name] => 东北林业大学 ) [1] => Array ( [id] => 12 [pid] => 8 [name] => 哈尔滨师范大学 ) ) ) ) ) [1] => Array ( [id] => 7 [pid] => 4 [name] => 南岗区 [son] => Array ( [0] => Array ( [id] => 9 [pid] => 7 [name] => 西大直街 [son] => Array ( [0] => Array ( [id] => 11 [pid] => 9 [name] => 哈尔滨工业大学 ) ) ) ) ) ) ) [1] => Array ( [id] => 5 [pid] => 2 [name] => 鸡西市 ) ) )
二、递归方式。(也是一次性先把数据查出来,再分类,个人更倾向于这种)
$array = array( array('id' => 1, 'pid' => 0, 'name' => '河北省'), array('id' => 2, 'pid' => 0, 'name' => '北京市'), array('id' => 3, 'pid' => 1, 'name' => '邯郸市'), array('id' => 4, 'pid' => 2, 'name' => '朝阳区'), array('id' => 5, 'pid' => 2, 'name' => '通州区'), array('id' => 6, 'pid' => 4, 'name' => '望京'), array('id' => 7, 'pid' => 4, 'name' => '酒仙桥'), array('id' => 8, 'pid' => 3, 'name' => '永年区'), array('id' => 9, 'pid' => 1, 'name' => '武安市'), );
/**
* 格式化树状结构分类
* @param array $data
* @param int $pid
* @return array|bool
*/
private function getTreeMenu($data = array(), $pid= 0)
{
if (empty($data)) {
return false;
}
$list = [];
foreach($data as $value)
{
if($value['pid'] == $pid)
{
$value['s_list'] = $this->getTreeMenu($data, $value['id']);
$list[] = $value;
}
}
return $list;
}
//输出结果 无限极分类实现大致效果
河北省
--邯郸市
----永年区
--武安市
北京市
--朝阳区
----望京
----酒仙桥
--通州区
三、带有数据库查询的递归(视情况而用)
public static function get_parents($id) { static $list = []; $cat_data = Category::findOne($id)->toarray(); if($cat_data){ $list[] = $cat_data; $id = $cat_data['parent_id']; if($cat_data['parent_id'] > 0){ self::get_parents($id); } } return $list; }
以上就是本篇全部内容了,仅供自己温故而知新。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/16392.html