子栏目递归问题
程序员文章站
2022-04-20 14:44:15
...
static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = ' ') {
$str_repeat = '';
if ($level) {
for($j = 0; $j $v ['id'], 'catalog_name' => $v ['catalog_name'], 'catalog_name_alias' => $v ['catalog_name_alias'], 'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'], 'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'], 'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'], 'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] , 'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],'template_show' => $v ['template_show'],'create_time' => $v ['create_time'], 'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );
$temparray = self::get ( $v ['id'], $array, ($level + $add) );
if ($temparray) {
$newarray = array_merge ( $newarray, $temparray );
}
}
}
return $newarray;
}
以上代码在Catalog model里,没理解错应该是递归获取子栏目这类的
现在我调用这货
Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')
返回array(0) { }
$catalogArr`打印出来的内容`array(22) { ["id"]=> string(2) "11" ["catalog_name"]=> string(12) "工作动态" ["catalog_name_alias"]=> string(4) "gzdt" ["parent_id"]=> string(1) "0" ["level"]=> int(0) ["sort_order"]=> string(1) "1" ["seo_keywords"]=> string(12) "工作动态" ["seo_description"]=> string(12) "工作动态" ["attach_file"]=> string(0) "" ["attach_thumb"]=> string(0) "" ["status_is"]=> string(1) "Y" ["data_count"]=> string(1) "0" ["display_type"]=> string(4) "list" ["menu_is"]=> string(1) "N" ["template_list"]=> string(9) "list_text" ["acl_browser"]=> string(0) "" ["acl_operate"]=> string(0) "" ["template_page"]=> string(9) "list_page" ["template_show"]=> string(9) "show_post" ["create_time"]=> string(10) "1409390084" ["str_repeat"]=> string(0) "" ["page_size"]=> string(2) "10" }
问我这样调用对吗》?为什么获取不了子栏目
回复内容:
static public function get($parentid = 0, $array = array(), $level = 0, $add = 2, $repeat = ' ') {
$str_repeat = '';
if ($level) {
for($j = 0; $j $v ['id'], 'catalog_name' => $v ['catalog_name'], 'catalog_name_alias' => $v ['catalog_name_alias'], 'parent_id' => $v ['parent_id'], 'level' => $level, 'sort_order' => $v ['sort_order'], 'seo_keywords' => $v ['seo_keywords'], 'seo_description' => $v ['seo_description'], 'attach_file' => $v ['attach_file'], 'attach_thumb' => $v ['attach_thumb'], 'status_is' => $v ['status_is'], 'data_count' => $v ['data_count'] , 'display_type' => $v ['display_type'], 'menu_is' => $v ['menu_is'],'template_list' => $v ['template_list'],'acl_browser' => $v ['acl_browser'],'acl_operate' => $v ['acl_operate'],'template_page' => $v ['template_page'],'template_show' => $v ['template_show'],'create_time' => $v ['create_time'], 'str_repeat' => $str_repeat, 'page_size'=>$v['page_size'] );
$temparray = self::get ( $v ['id'], $array, ($level + $add) );
if ($temparray) {
$newarray = array_merge ( $newarray, $temparray );
}
}
}
return $newarray;
}
以上代码在Catalog model里,没理解错应该是递归获取子栏目这类的
现在我调用这货
Catalog::get($catalogArr['id'],$catalogArr,2,2,'>>')
返回array(0) { }
$catalogArr`打印出来的内容`array(22) { ["id"]=> string(2) "11" ["catalog_name"]=> string(12) "工作动态" ["catalog_name_alias"]=> string(4) "gzdt" ["parent_id"]=> string(1) "0" ["level"]=> int(0) ["sort_order"]=> string(1) "1" ["seo_keywords"]=> string(12) "工作动态" ["seo_description"]=> string(12) "工作动态" ["attach_file"]=> string(0) "" ["attach_thumb"]=> string(0) "" ["status_is"]=> string(1) "Y" ["data_count"]=> string(1) "0" ["display_type"]=> string(4) "list" ["menu_is"]=> string(1) "N" ["template_list"]=> string(9) "list_text" ["acl_browser"]=> string(0) "" ["acl_operate"]=> string(0) "" ["template_page"]=> string(9) "list_page" ["template_show"]=> string(9) "show_post" ["create_time"]=> string(10) "1409390084" ["str_repeat"]=> string(0) "" ["page_size"]=> string(2) "10" }
问我这样调用对吗》?为什么获取不了子栏目
贴一个我的代码:
/**
* 返回多层栏目
* @param $data 操作的数组
* @param int $pid 一级PID的值
* @param string $html 栏目名称前缀
* @param string $fieldPri 唯一键名,如果是表则是表的主键
* @param string $fieldPid 父ID键名
* @param int $level 不需要传参数(执行时调用)
* @return array
*/
static public function channelLevel($data, $pid = 0, $html = " ", $fieldPri = 'cid', $fieldPid = 'pid', $level = 1)
{
if (empty($data)) {
return array();
}
$arr = array();
foreach ($data as $v) {
if ($v[$fieldPid] == $pid) {
$arr[$v[$fieldPri]] = $v;
$arr[$v[$fieldPri]]['_level'] = $level;
$arr[$v[$fieldPri]]['_html'] = str_repeat($html, $level - 1);
$arr[$v[$fieldPri]]["_data"] = self::channelLevel($data, $v[$fieldPri], $html, $fieldPri, $fieldPid, $level + 1);
}
}
return $arr;
}
楼主可以看下
上一篇: 第一篇博文 - web工匠
下一篇: Python中的异常处理简明介绍
推荐阅读
-
dedecms中,获取某一栏目所有子栏目
-
请问 预排序遍历树算法(非递归无限极分类算法) ,删除一个分支节点和它所有的子节点有关问题
-
python的描述符(descriptor)、装饰器(property)造成的一个无限递归问题分享
-
关于递归调用的问题
-
递归函数使用动态数组遇到的问题
-
php-新手问题,有关递归的
-
MySQL子查询中order by不生效问题的解决方法
-
Latex子图序号按全局排序而非局部排序的问题
-
sqlserver in 子查询问题
-
POJ3984 迷宫问题记录路径递归 bfs HDU1242 dfs Codeforces25D.Roads in Berland floyd优化 HDU1874畅通工程续 floyd/spfa/dj