unlimited classification, mainly by storing the id and classification path of the upper-level classification
In my Simpla, infinite classification is used, and PHP's passing idea is used to achieve infinite classification, which can perfectly display classification patterns like this.
id pid name
1 0 Sichuan
2 0 Chongqing
3 1 Chengdu
4 1 Mianyang
5 3 High-tech Zone
The code is as follows:
/** * 数组变成无限级分类--传引用思想 * @param array $items * @return array */ public static function get_tree($orig) { //解决下标不是1开始的问题 $items = array(); foreach ($orig as $key => $value) { $items[$value[‘id‘]] = $value; } //开始组装 $tree = array(); foreach ($items as $key => $item) { if ($item[‘pid‘] == 0) { //为0,则为1级分类 $tree[] = &$items[$key]; } else { if (isset($items[$item[‘pid‘]])) { //存在值则为二级分类 $items[$item[‘pid‘]][‘child‘][] = &$items[$key]; //传引用直接赋值与改变 } else { //至少三级分类 //由于是传引用思想,这里将不会有值 $tree[] = &$items[$key]; } } } return $tree; }
The above content is very simple. If there are any mistakes or better methods, I hope we can communicate with each other. Thanks. !