function
get_child_category(
$categories
,
$parent_id
=0){
static
$arr
=
array
();
foreach
(
$categories
as
$category
){
if
(
$category
[
'parent_id'
]==
$parent_id
){
$arr
[]=
$category
;
get_child_category(
$categories
,
$category
[
'cat_id'
]);
}
}
return
$arr
;
}
//子类操作
function z_add($sort_name,$sort_pid=0,$fj=0){
$arr=array();
foreach ($sort_name as $v) {
if($v['sort_pid']==$sort_pid){
$v['fj']=$fj;
$v['html']=str_repeat('|**',$fj);
$arr[]=$v;
$arr=array_merge($arr,$this->z_add($sort_name,$v['sort_id'],$fj+1));
}
}
return $arr;
}
function zadd($goods_class,$goods_pid=0,$fj=0){
$arr=array();
foreach($goods_class as $v){
if($v['goods_pid']==$goods_pid){
$v['fj']=$fj;
$v['html']=str_repeat('|**',$fj);
$arr[]=$v;
$arr=array_merge($arr,$this->zadd($goods_class,$v['goods_id'],$fj+1));
}
}
return $arr;
}
- <?php
-
-
-
-
-
-
-
- header("content-type:text/html;charset=utf-8");
- $categories = array(
- array('id'=>1,'name'=>'电脑','pid'=>0),
- array('id'=>2,'name'=>'手机','pid'=>0),
- array('id'=>3,'name'=>'笔记本','pid'=>1),
- array('id'=>4,'name'=>'台式机','pid'=>1),
- array('id'=>5,'name'=>'智能机','pid'=>2),
- array('id'=>6,'name'=>'功能机','pid'=>2),
- array('id'=>7,'name'=>'超级本','pid'=>3),
- array('id'=>8,'name'=>'游戏本','pid'=>3),
- );
-
-
- $tree = array();
-
- foreach($categories as $category){
- $tree[$category['id']] = $category;
- $tree[$category['id']]['children'] = array();
- }
-
- foreach($tree as $key=>$item){
- if($item['pid'] != 0){
- $tree[$item['pid']]['children'][] = &$tree[$key];
- if($tree[$key]['children'] == null){
- unset($tree[$key]['children']);
- }
- }
- }
-
- foreach($tree as $key=>$category){
- if($category['pid'] != 0){
- unset($tree[$key]);
- }
- }
-
- print_r($tree);
-
-
- $tree = $categories;
- function get_attr($a,$pid){
- $tree = array();
- foreach($a as $v){
- if($v['pid'] == $pid){
- $v['children'] = get_attr($a,$v['id']);
- if($v['children'] == null){
- unset($v['children']);
- }
- $tree[] = $v;
- }
- }
- return $tree;
- }
- echo "<br/><br/><br/>";
-
- print_r(get_attr($tree,0));