数据库设计,与数据填入:
打印原始数据:
所有数据 Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 ) [1] => Array ( [id] => 9 [name] => Ajax [pid] => 3 [sort] => 1 ) [2] => Array ( [id] => 11 [name] => 字符串 [pid] => 5 [sort] => 1 ) [3] => Array ( [id] => 2 [name] => DIV+CSS [pid] => 0 [sort] => 2 ) [4] => Array ( [id] => 8 [name] => Jquery [pid] => 3 [sort] => 2 ) [5] => Array ( [id] => 10 [name] => 函数 [pid] => 5 [sort] => 2 ) [6] => Array ( [id] => 3 [name] => JavaScript [pid] => 0 [sort] => 3 ) [7] => Array ( [id] => 4 [name] => Apache [pid] => 0 [sort] => 4 ) [8] => Array ( [id] => 1 [name] => HTML [pid] => 0 [sort] => 5 ) [9] => Array ( [id] => 6 [name] => Linux [pid] => 0 [sort] => 6 ) [10] => Array ( [id] => 7 [name] => 其他 [pid] => 0 [sort] => 7 ) )
使用算法得到数据:
第一种最常用(常用于导航分类):
第二种(常用于后台显示分类列表):
/** * 数据融合函数,将其自己数据放置父级之后,组成一维数组 * @param array $cate 原始数据 * @param integer $pid 父级id * @param string $name 子级数组的键名 * @return array $arr 组合后数组 */ static function mergeCateToTwoArray($cate, $pid = 0, $name = 'child') { $arr = array(); foreach ( $cate as $v ) { if ( $v['pid'] == $pid ) { $v[$name] = self::mergeCateToTwoArray($cate, $v['id'], $name); $arr[] = $v; } } return $arr; } 调用: //将数据组合为多维数组 // $cate = Category::mergeCateToTwoArray($cate, 0, 'child'); 结果: Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 [child] => Array ( [0] => Array ( [id] => 11 [name] => 字符串 [pid] => 5 [sort] => 1 [child] => Array ( ) ) [1] => Array ( [id] => 10 [name] => 函数 [pid] => 5 [sort] => 2 [child] => Array ( ) ) ) ) [1] => Array ( [id] => 2 [name] => DIV+CSS [pid] => 0 [sort] => 2 [child] => Array ( ) ) [2] => Array ( [id] => 3 [name] => JavaScript [pid] => 0 [sort] => 3 [child] => Array ( [0] => Array ( [id] => 9 [name] => Ajax [pid] => 3 [sort] => 1 [child] => Array ( ) ) [1] => Array ( [id] => 8 [name] => Jquery [pid] => 3 [sort] => 2 [child] => Array ( ) ) ) ) [3] => Array ( [id] => 4 [name] => Apache [pid] => 0 [sort] => 4 [child] => Array ( ) ) [4] => Array ( [id] => 1 [name] => HTML [pid] => 0 [sort] => 5 [child] => Array ( ) ) [5] => Array ( [id] => 6 [name] => Linux [pid] => 0 [sort] => 6 [child] => Array ( ) ) [6] => Array ( [id] => 7 [name] => 其他 [pid] => 0 [sort] => 7 [child] => Array ( ) ) ) //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * 数据融合函数,将其自己数据放置父级之后,组成一维数组 * @param array $cate 原始数据 * @param integer $pid 父级id * @param integer $level 所属等级 * @param string $html 分割符号 * @return array $arr 返回整理好的数据 */ static function mergeCateToOneArray($cate, $pid = 0, $level = 0, $html = '--') { $arr = array(); foreach ( $cate as $v ) { if ( $v['pid'] == $pid ) { $v['level'] = $level + 1; $v['html'] = str_repeat($html, $level); $arr[] = $v; //将这一次递归,看成得到的一个数据 $arr = array_merge($arr, self::mergeCateToOneArray($cate, $v['id'], $level+1, $html)); } } return $arr; } 调用: //将数据组合为一维数组 // $cate = Category::mergeCateToOneArray($cate, 0, 0, ' --'); 结果: Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 [level] => 1 [html] => ) [1] => Array ( [id] => 11 [name] => 字符串 [pid] => 5 [sort] => 1 [level] => 2 [html] => -- ) [2] => Array ( [id] => 10 [name] => 函数 [pid] => 5 [sort] => 2 [level] => 2 [html] => -- ) [3] => Array ( [id] => 2 [name] => DIV+CSS [pid] => 0 [sort] => 2 [level] => 1 [html] => ) [4] => Array ( [id] => 3 [name] => JavaScript [pid] => 0 [sort] => 3 [level] => 1 [html] => ) [5] => Array ( [id] => 9 [name] => Ajax [pid] => 3 [sort] => 1 [level] => 2 [html] => -- ) [6] => Array ( [id] => 8 [name] => Jquery [pid] => 3 [sort] => 2 [level] => 2 [html] => -- ) [7] => Array ( [id] => 4 [name] => Apache [pid] => 0 [sort] => 4 [level] => 1 [html] => ) [8] => Array ( [id] => 1 [name] => HTML [pid] => 0 [sort] => 5 [level] => 1 [html] => ) [9] => Array ( [id] => 6 [name] => Linux [pid] => 0 [sort] => 6 [level] => 1 [html] => ) [10] => Array ( [id] => 7 [name] => 其他 [pid] => 0 [sort] => 7 [level] => 1 [html] => ) ) //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * 根据子类id获取父类id * @param [type] $cate [description] * @param [type] $id [description] * @return [type] [description] */ static function getParents($cate, $id) { $arr = array(); foreach ( $cate as $v ) { if ( $v['id'] == $id ) { $arr[] = $v; $arr = array_merge(self::getParents($cate, $v['pid']), $arr); } } return $arr; } 调用: $cate = Category::getParents($cate, 11); 结果: Array ( [0] => Array ( [id] => 5 [name] => PHP [pid] => 0 [sort] => 1 ) [1] => Array ( [id] => 11 [name] => 字符串 [pid] => 5 [sort] => 1 ) ) //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ /** * 根据父类 ID 获取子级ID * @return [type] [description] */ static function getChildsId ($cate, $pid) { $arr = array(); foreach ( $cate as $v ) { if ( $v['pid'] == $pid ) { $arr[] = $v['id']; $arr = array_merge($arr, self::getChildsId($cate, $v['id'])); } } return $arr; } 调用: //根据父类id查找其下子级元素 $cate = Category::getChildsId($cate, 5); 结果: Array ( [0] => 11 [1] => 10 )