Rumah > pembangunan bahagian belakang > tutorial php > php之无极限分类函数

php之无极限分类函数

小云云
Lepaskan: 2023-03-21 12:16:01
asal
1518 orang telah melayarinya

本文主要和大家分享php之无极限分类函数吗,希望能帮助到大家。

/**
 * [make_tree description]
 * @Author   Lerko
 * @DateTime 2017-04-01T14:57:24+0800
 * @param    [type]                   $list      [所有的数据]
 * @param    [type]                   &$data     [返回的数据的载体,传空数组]
 * @param    string                   $pk        [默认主键]
 * @param    string                   $pid       [关联到主键的key]
 * @param    string                   $child_key [孩子节点的数据key]
 * @param    string                   $sort_id   [排序id]
 * @return   [type]                              [description]
 */
function pidToGetTree($list,&$data,$pk='id',$pid='pid',$child_key="_child",$sort_id='sort_id',$sort_type=SORT_ASC){
    if($data===null){
        return;
    }
    if(count($data)==0){
        //初始化根列表
        foreach ($list as $key => &$value) {
            if($value[$pid]==0){
                $data[]=$value;
                unset($list[$key]);
            }
        }
    }
    foreach ($data as $key => &$value) {
        foreach ($list as $key2 => $value2) {
            if($value2[$pid]==$value[$pk]){
                $value[$child_key][]=$value2;
                unset($list[$key2]);
            }
        }
        if($value[$child_key]){continue;}
        $sort_arr=array_column($value[$child_key],$sort_id);
        array_multisort($sort_arr,$sort_type,$value[$child_key]);
        pidToGetTree($list,$value[$child_key]);
    }
}
Salin selepas log masuk


输入数据

+----+-----+---------+---------+---------+-------------+---------+
| id | pid | root_id | user_id | sort_id | name_cn     | name_en |
+----+-----+---------+---------+---------+-------------+---------+
|  1 |   0 |    2160 |    2160 |       0 | 种类1       | cat     |
|  2 |   1 |    2160 |    2160 |       1 | 种类1-1     | cat     |
|  3 |   1 |    2160 |    2160 |       1 | 种类1-2     | cat     |
|  4 |   1 |    2160 |    2160 |       1 | 种类1-3     | cat     |
|  5 |   0 |    2160 |    2160 |       0 | 种类5       | cat     |
|  6 |   2 |    2160 |    2160 |       2 | 种类1-1-1   | cat     |
|  7 |   5 |    2160 |    2160 |       5 | 种类5-1     | cat     |
|  8 |   5 |    2160 |    2160 |       3 | 种类5-2     | cat     |
|  9 |   0 |    2160 |    2160 |       0 | 种类9       | cat     |
| 10 |   9 |    2160 |    2160 |       9 | 种类9-1     | cat     |
| 11 |   9 |    2160 |    2160 |       9 | 种类9-2     | cat     |
+----+-----+---------+---------+---------+-------------+---------+
Salin selepas log masuk

输出数据

array (size=3)
  0 =>
    array (size=8)
      'id' => string '1' (length=1)
      'pid' => string '0' (length=1)
      'root_id' => string '2160' (length=4)
      'user_id' => string '2160' (length=4)
      'sort_id' => string '0' (length=1)
      'name_cn' => string '种类1' (length=7)
      'name_en' => string 'cat' (length=3)
      '_child' =>
        array (size=3)
          0 =>
            array (size=7)
              'id' => string '2' (length=1)
              'pid' => string '1' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '1' (length=1)
              'name_cn' => string '种类1-1' (length=9)
              'name_en' => string 'cat' (length=3)
          1 =>
            array (size=7)
              'id' => string '3' (length=1)
              'pid' => string '1' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '1' (length=1)
              'name_cn' => string '种类1-2' (length=9)
              'name_en' => string 'cat' (length=3)
          2 =>
            array (size=7)
              'id' => string '4' (length=1)
              'pid' => string '1' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '1' (length=1)
              'name_cn' => string '种类1-3' (length=9)
              'name_en' => string 'cat' (length=3)
  1 =>
    array (size=8)
      'id' => string '5' (length=1)
      'pid' => string '0' (length=1)
      'root_id' => string '2160' (length=4)
      'user_id' => string '2160' (length=4)
      'sort_id' => string '0' (length=1)
      'name_cn' => string '种类5' (length=7)
      'name_en' => string 'cat' (length=3)
      '_child' =>
        array (size=2)
          0 =>
            array (size=7)
              'id' => string '7' (length=1)
              'pid' => string '5' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '5' (length=1)
              'name_cn' => string '种类5-1' (length=9)
              'name_en' => string 'cat' (length=3)
          1 =>
            array (size=7)
              'id' => string '8' (length=1)
              'pid' => string '5' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '3' (length=1)
              'name_cn' => string '种类5-2' (length=9)
              'name_en' => string 'cat' (length=3)
  2 =>
    array (size=8)
      'id' => string '9' (length=1)
      'pid' => string '0' (length=1)
      'root_id' => string '2160' (length=4)
      'user_id' => string '2160' (length=4)
      'sort_id' => string '0' (length=1)
      'name_cn' => string '种类9' (length=7)
      'name_en' => string 'cat' (length=3)
      '_child' =>
        array (size=2)
          0 =>
            array (size=7)
              'id' => string '10' (length=2)
              'pid' => string '9' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '9' (length=1)
              'name_cn' => string '种类9-1' (length=9)
              'name_en' => string 'cat' (length=3)
          1 =>
            array (size=7)
              'id' => string '11' (length=2)
              'pid' => string '9' (length=1)
              'root_id' => string '2160' (length=4)
              'user_id' => string '2160' (length=4)
              'sort_id' => string '9' (length=1)
              'name_cn' => string '种类9-2' (length=9)
              'name_en' => string 'cat' (length=3)
Salin selepas log masuk

相关推荐:

PHP+MySQL实现无极限分类栏目的方法,_PHP教程

PHP+MySQL实现无极限分类栏目的方法_PHP

PHP+MySQL实现无极限分类栏目的方法_php技巧


Atas ialah kandungan terperinci php之无极限分类函数. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan