验证中...
语言: PHP
分类: Web开发技术
最后更新于 2018-06-14 18:53
gistfile1.txt
原始数据 复制代码
<?php
/**
* Created by PhpStorm.
* User: chao
* Date: 2016/10/22
* Time: 下午9:25
*/
function dd(){
$params = func_get_args()?:null;
foreach ($params as $k=> $param){
var_dump($param);
echo "<hr/>";
}
die;
}
$datas = 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'=>1,'name'=>'二级栏目二'),
5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二'),
);
function findChilds($rows, $pid=0)
{
$lists = array();
if (is_array($rows)) {
foreach ($rows as $k => $row) {
if ($row['pid'] == $pid) $lists[$row['id']] = $row;
}
}
return $lists;
}
function buildTree($rows, $pid=0, $level = 1,$lprefix='_level',$cprefix = '_child')
{
$lists = findChilds($rows, $pid,$level);
if (is_array($lists)) {
foreach ($lists as $k => &$v) {
$v[$lprefix] = $level;
$tmp = buildTree($rows, $v['id'],$level + 1);
if (null != $tmp) {
$lists[$k][$cprefix] = $tmp;
}
}
}
return $lists;
}
// dd(buildTree($datas));
function showTree($trees, $title = 'name',$lprefix = '_level', $cprefix = '_child',$icon="|---")
{
$formatTree = array();
foreach($trees as $key => $tree){
$title_prefix = '';
if ($tree['pid'] > 0) {
$title_prefix = str_repeat($icon, $tree[$lprefix] - 1);
}
$tree['_showName'] = $title_prefix.$tree[$title];
if(!array_key_exists($cprefix, $tree)){
array_push($formatTree, $tree);
}else{
$child = $tree[$cprefix];
unset($tree[$cprefix]);
array_push($formatTree, $tree);
$middle = showTree($child); //进行下一层递归
$formatTree = array_merge($formatTree, $middle);
}
}
return $formatTree;
}
// $trees = buildTree($datas);
// $trees = showTree($trees);
// dd($trees);
// $html = '<select name="" id="">';
// foreach ($trees as $key => $row) {
// $html .= '<option value="'.$row['id'].'"> '.$row['_showName'].' </option> ';
// }
// $html.= '</select>';
// echo ($html);
function buildTree2($arr, $pid = 0, $level = 1,$prefix='_level')
{
static $lists = array();
foreach ($arr as $v) {
//如果是顶级分类,则将其存到$list中,并以此节点为根节点,遍历其子节点
if ($v['pid'] == $pid) {
$v[$prefix] = $level;
$lists[$v['id']] = $v;
buildTree2($arr, $v['id'], $level + 1);
}
}
return $lists;
}
function buildTree3($rows, $pid=0,$level = 1,$prefix='_level')
{
static $lists = array();
$childs = findChilds($rows, $pid);
if (is_array($childs)) {
foreach ($childs as $k => $v) {
$v[$prefix] = $level;
$lists[$v['id']] = $v;
buildTree3($rows, $v['id'],$level + 1);
}
}
return $lists;
}
/**
* level法排序(前提是数组顺序已经定了)
* @param $rows
* @param string $is_show
* @return mixed
*/
function showTree2($rows, $title = 'name',$lprefix = '_level', $cprefix = '_child',$icon="|---")
{
foreach ($rows as $k => &$row) {
$title_prefix = '';
if ($row[$lprefix] > 1) {
$title_prefix = str_repeat($icon, $row[$lprefix] - 1);
}
$row['_showName'] = $title_prefix . $row[$title];
}
return $rows;
}
// $trees = buildTree2($datas);
// $trees = buildTree3($datas);
// $trees = showTree2($trees);
// // dd($trees);
// $html = '<select name="" id="">';
// foreach ($trees as $key => $row) {
// $html .= '<option value="'.$row['id'].'"> '.$row['_showName'].' </option> ';
// }
// $html.= '</select>';
// echo ($html);
/**
* 把返回的数据集转换成Tree
* @param $list
* @param string $pk
* @param string $pid
* @param string $child
* @param string $root
* @return array
*/
function listToTree($list, $pk='id', $pid = 'pid', $child = '_child', $root = '0') {
$tree = array();
if(is_array($list)) {
$refer = array();
foreach ($list as $key => $data) {
$refer[$data[$pk]] = &$list[$key];
}
foreach ($list as $key => $data) {
$parentId = $data[$pid];
if ($root == $parentId) {
$tree[] = &$list[$key];
}else{
if (isset($refer[$parentId])) {
$parent = &$refer[$parentId];
$parent[$child][] = &$list[$key];
}
}
}
}
return $tree;
}
function formatTree($list, $lv = 0, $title = 'name'){
$formatTree = array();
foreach($list as $key => $val){
$title_prefix = '';
for( $i=0;$i<$lv;$i++ ){
$title_prefix .= "|---";
}
$val['lv'] = $lv;
$val['namePrefix'] = $lv == 0 ? '' : $title_prefix;
$val['showName'] = $lv == 0 ? $val[$title] : $title_prefix.$val[$title];
if(!array_key_exists('_child', $val)){
array_push($formatTree, $val);
}else{
$child = $val['_child'];
unset($val['_child']);
array_push($formatTree, $val);
$middle = formatTree($child, $lv+1, $title); //进行下一层递归
$formatTree = array_merge($formatTree, $middle);
}
}
return $formatTree;
}
// dd(listToTree($datas));
// dd(formatTree(listToTree($datas)));

评论列表( 0 )

你可以在登录后,发表评论

搜索帮助