/**
* 组合一维数组
* @param [type] $cate [要处理的数组]
* @param string $html [默认的字符串]
* @param integer $pid [默认为0,将id=pid数组里面压]
* @param integer $level [等级,默认0,回调+1]
* @return [type] $cate [返回处理完成的数组]
* Name xiaoke
*/
function unlimitedForLevel ($cate, $html = '', $pid = 0, $level = 0){
$arr = array();
foreach ($cate as $v) {
if ($v['parentid'] == $pid) {
$v['level'] = $level + 1;
$v['html'] = str_repeat($html, $level);
$arr[] = $v;
//合并成一个数组
$arr = array_merge($arr, unlimitedForLevel($cate, $html, $v['id'], $level + 1));
}
}
return $arr;
}
/**
* 组合多维数组
* @param [type] $cate [要处理的数组]
* @param string $name [默认的值]
* @param integer $pid [默认为0,将id=pid数组里面压]
* @return [type] [返回处理完成的数组]
* Name xiaoke
*/
function GetLayer ($cate, $pid = 0, $name = 'lower',$html="--",$level=0){
$arr = array();
foreach ($cate as $v) {
if ($v['pid'] == $pid) {
$v['deep'] = $level;
$v['html'] = str_repeat($html, $level);
$v[$name] = GetLayer($cate, $v['id'], $name, $html, $level+1);
$arr[] = $v;
}
}
return $arr;
}
/**
* 子级ID返回父级分类
* @param [type] $cate [要处理的数组]
* @param [type] $id [子级ID]
* @return [type] [返回处理完成的数组]
*/
function getParents ($cate, $id) {
$arr = array();
foreach ($cate as $v) {
if ($v['pid'] == $id) {
$arr[] = $v;
$arr = array_merge(getParents($cate, $v['t_pid']), $arr);
}
}
return $arr;
}
/**
* 传递一个父级分类ID返回所有子级分类
* @param [type] $cate [要处理的数组]
* @param [type] $id [父级ID]
* @return [type] [返回处理完成的数组]
*/
function getChilds ($cate, $pid) {
$arr = array();
foreach ($cate as $v) {
if ($v['t_pid'] == $pid) {
$arr[] = $v;
$arr = array_merge($arr, getChilds($cate, $v['pid']));
}
}
return $arr;
}玄玄博客:http://www.blogxuan.com/