Detailed explanation of two methods of implementing stepless recursive classification in PHP

迷茫
Release: 2023-03-06 22:38:02
Original
1141 people have browsed it

Two ways to implement stepless recursive classification in PHP

The first one:

/** * 无级递归分类 TP框架 * @param int $assortPid 要查询分类的父级id * @param mixed $tag 上下级分类之间的分隔符 * @return string $tree 返回的分类树型结构结果 * */ function recursiveAssort($assortPid, $tag = '') { $assort = M('goods_class')->where("class_pid = $assortPid")->field('class_id, class_name')->select(); foreach ($assort as $value) { $tree .= ''; $tree .= recursiveAssort($value['class_id'], $tag . ' '); } return $tree; }
Copy after login

Second type:

/** * 利用php的引用传递 CI框架 * */ public function get_access() { $access = array(); $field = 'id, pid, method, name, description'; $q_access = $this->db->select($field)->get('access'); $q_result = $q_access->result_array(); if (!empty($q_result)) { $items = array(); foreach ($q_result as $value) { $items[$value['id']] = $value; } foreach ($items as $key => $item) { if ($item['pid'] == 0) { $access[] = &$items[$key]; } else { $items[$item['pid']]['sub_access'][] = &$items[$key]; } } } return $access; }
Copy after login

The above is the detailed content of Detailed explanation of two methods of implementing stepless recursive classification in PHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!