如何在最多 4 个级别的树中选择类别

编程入门 行业动态 更新时间:2024-10-14 04:30:28
本文介绍了如何在最多 4 个级别的树中选择类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试创建层次结构/树中所有类别的二维数组.

I am trying to create two dimensional array of all categories in hierarchy/tree.

因此,先是父类别,然后是其子类别,然后是第 3 级类别,然后是第 4 级.

So parent come first then its subcategories then 3rd level categories and then 4th level.

我有这样的记录表

cc_id category_name parent_id level 1 general 0 1 2 Books 1 2 3 Magazines 1 2 4 English Book 2 3 5 Love Story 4 4

我想要这样的输出

Array ( [0] => Array ( [cc_id] => 1 [parent_id] => 0 [category_name] => General [level] => 1 ) [1] => Array ( [cc_id] => 2 [parent_id] => 1 [category_name] => Books [level] => 2 ) [2] => Array ( [cc_id] => 4 [parent_id] => 2 [category_name] => English Book [level] => 3 ) [3] => Array ( [cc_id] => 4 [parent_id] => 3 [category_name] => Love Story [level] => 4 ) [4] => Array ( [cc_id] => 3 [parent_id] => 1 [category_name] => Magazine [level] => 2 ) )

如果我可以在同一页面上混合 html 和 php 代码,我可以实现这一点.但我使用的是 MVC 结构,所以这是我的要求,我以嵌套顺序创建一个数组.因为我不能在我的内部使用 if else 条件查看页面.

I can achieve this If I can mixup html and php code on the same page.But I am using MVC structure so this is my requirement I create a array in nested order.Because I can't use if else conditions inside my view page.

我在控制器/lib 文件中尝试的是

What I am trying in my controller/lib file is

$result = $DB->query("SELECT * FROM " . DB_PREFIX . "classified_categoies WHERE is_publish=1"); //------------------------------------------------ // Check if resultset contains any rows //------------------------------------------------ if ($DB->num_rows($result)) { $i = 0; //------------------------------------------------ // Fetch resultset //------------------------------------------------ while( $obj = $DB->fetch_object($result) ) { //------------------------------------------------ // Set values //------------------------------------------------ if($obj->parent_id==0 and $obj->level==1) { $entries_obj[$i]['category_id'] = $obj->cc_id; $entries_obj[$i]['parent_id'] = $obj->parent_id; $entries_obj[$i]['category_name'] = $obj->category_name; $entries_obj[$i]['level'] = $obj->level; $i++; } $result2 = $DB->query("SELECT * FROM " . DB_PREFIX . "classified_categoies WHERE is_publish=1 AND parent_id=".$obj->cc_id." order by level asc"); if ($DB->num_rows($result)) { while( $obj2 = $DB->fetch_object($result2) ) { if($obj->level==2) { $entries_obj[$i]['category_id'] = $obj2->cc_id; $entries_obj[$i]['parent_id'] = $obj2->parent_id; $entries_obj[$i]['category_name'] = $obj2->category_name; $entries_obj[$i]['level'] = $obj2->level; $i++; } } } }

但是通过这样做,我最多可以得到 2 个级别的类别.似乎通过使用上述逻辑,我必须编写 4 个查询.你们有没有任何有效的方法可以提出建议.谢谢..

But by doing this I am able to get upto 2 level categories.It seems by using above logic I have to written 4 queries.Is there not any efficient way you people can suggest Please.thank you..

一般输出应该是这样的

Genaral Books English books Love Story Magazines

但我想要二维数组的输出.

But I want output in two dimensional array.

推荐答案

递归方法可以做到...

A recursive method would do it...

// First get a simple array of recordset, not as efficient but serves our purpose, so avoid filtering by level... $baseArray = array(); while( $obj = $DB->fetch_object($result) ) { $baseArray[$obj->cc_id] = new array('category_id' => $obj->cc_id, 'category_name' => $obj->category_name, 'parent_id' => $obj->parent_id, 'level' => $obj->level); } // Now our recursive method function getBookCategories($inputArray, $parent = 0, $level = 0){ $tmpReturn = array(); foreach($inputArray as $cc_id => $categoryArray){ if($categoryArray['parent_id'] == $parent){ $tmpReturn[$cc_id] = $categoryArray; if($level < 4){ $tmpReturn[$cc_id]['children'] = array(); $tmpReturn[$cc_id]['children'] = getBookCategories($inputArray, $cc_id, $level + 1); } } } return $tmpReturn; }

编辑

如果您希望它与 $baseArray 相同,则:

If you wish it the same as $baseArray then:

function getBookCategories2($inputArray, &$outputArray, $parent = 0, $level = 0){ foreach($inputArray as $cc_id => $categoryArray){ if($categoryArray['parent_id'] == $parent){ $outputArray[] = $categoryArray; if($level < 4){ getBookCategories2($inputArray, $outputArray, $cc_id, $level + 1); } } } } $desiredArray = array(); getBookCategories2($baseArray, $desiredArray, 0, 0);

更多推荐

如何在最多 4 个级别的树中选择类别

本文发布于:2023-11-30 09:02:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1649451.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:最多   级别   类别   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!