当前位置:Gxlcms > PHP教程 > 三级分类like查询查询到很多id需要找到对应的父级id并根据父级id组合应该怎么根据父级id将数据组合在一起呢?

三级分类like查询查询到很多id需要找到对应的父级id并根据父级id组合应该怎么根据父级id将数据组合在一起呢?

时间:2021-07-01 10:21:17 帮助过:18人阅读

这是获取到的数据 父级id为parent_id

Array
(
    [0] => Array
        (
            [department_id] => 1
            [parent_id] => 0
            [department_code] => xsb1
            [department_name] => 销售部1
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [1] => Array
        (
            [department_id] => 2
            [parent_id] => 1
            [department_code] => xs1q
            [department_name] => 销售1区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [2] => Array
        (
            [department_id] => 3
            [parent_id] => 2
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [3] => Array
        (
            [department_id] => 4
            [parent_id] => 2
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [4] => Array
        (
            [department_id] => 5
            [parent_id] => 2
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [5] => Array
        (
            [department_id] => 6
            [parent_id] => 2
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [6] => Array
        (
            [department_id] => 7
            [parent_id] => 1
            [department_code] => xs2q
            [department_name] => 销售2区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [7] => Array
        (
            [department_id] => 8
            [parent_id] => 7
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [8] => Array
        (
            [department_id] => 9
            [parent_id] => 7
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [9] => Array
        (
            [department_id] => 10
            [parent_id] => 7
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [10] => Array
        (
            [department_id] => 11
            [parent_id] => 7
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [11] => Array
        (
            [department_id] => 12
            [parent_id] => 0
            [department_code] => xsb2
            [department_name] => 销售部2
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [12] => Array
        (
            [department_id] => 13
            [parent_id] => 12
            [department_code] => xs3q
            [department_name] => 销售3区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [13] => Array
        (
            [department_id] => 14
            [parent_id] => 13
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [14] => Array
        (
            [department_id] => 15
            [parent_id] => 13
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [15] => Array
        (
            [department_id] => 16
            [parent_id] => 13
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [16] => Array
        (
            [department_id] => 17
            [parent_id] => 13
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [17] => Array
        (
            [department_id] => 18
            [parent_id] => 12
            [department_code] => xs4q
            [department_name] => 销售4区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [18] => Array
        (
            [department_id] => 19
            [parent_id] => 18
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [19] => Array
        (
            [department_id] => 20
            [parent_id] => 18
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

    [20] => Array
        (
            [department_id] => 21
            [parent_id] => 18
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

    [21] => Array
        (
            [department_id] => 22
            [parent_id] => 18
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

)

这个还是三级分类有可能搜到的是最后一级 那还要找俩级 怎么去合并啊

回复内容:

这是获取到的数据 父级id为parent_id

Array
(
    [0] => Array
        (
            [department_id] => 1
            [parent_id] => 0
            [department_code] => xsb1
            [department_name] => 销售部1
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [1] => Array
        (
            [department_id] => 2
            [parent_id] => 1
            [department_code] => xs1q
            [department_name] => 销售1区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [2] => Array
        (
            [department_id] => 3
            [parent_id] => 2
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [3] => Array
        (
            [department_id] => 4
            [parent_id] => 2
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [4] => Array
        (
            [department_id] => 5
            [parent_id] => 2
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [5] => Array
        (
            [department_id] => 6
            [parent_id] => 2
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [6] => Array
        (
            [department_id] => 7
            [parent_id] => 1
            [department_code] => xs2q
            [department_name] => 销售2区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [7] => Array
        (
            [department_id] => 8
            [parent_id] => 7
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [8] => Array
        (
            [department_id] => 9
            [parent_id] => 7
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [9] => Array
        (
            [department_id] => 10
            [parent_id] => 7
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [10] => Array
        (
            [department_id] => 11
            [parent_id] => 7
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [11] => Array
        (
            [department_id] => 12
            [parent_id] => 0
            [department_code] => xsb2
            [department_name] => 销售部2
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [12] => Array
        (
            [department_id] => 13
            [parent_id] => 12
            [department_code] => xs3q
            [department_name] => 销售3区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [13] => Array
        (
            [department_id] => 14
            [parent_id] => 13
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [14] => Array
        (
            [department_id] => 15
            [parent_id] => 13
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [15] => Array
        (
            [department_id] => 16
            [parent_id] => 13
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [16] => Array
        (
            [department_id] => 17
            [parent_id] => 13
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [17] => Array
        (
            [department_id] => 18
            [parent_id] => 12
            [department_code] => xs4q
            [department_name] => 销售4区
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [18] => Array
        (
            [department_id] => 19
            [parent_id] => 18
            [department_code] => xs1ks
            [department_name] => 销售1科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039207
            [add_user_id] => 1
            [edit_time] => 1470039207
            [edit_user_id] => 1
        )

    [19] => Array
        (
            [department_id] => 20
            [parent_id] => 18
            [department_code] => xs2ks
            [department_name] => 销售2科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

    [20] => Array
        (
            [department_id] => 21
            [parent_id] => 18
            [department_code] => xs3ks
            [department_name] => 销售3科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

    [21] => Array
        (
            [department_id] => 22
            [parent_id] => 18
            [department_code] => xs4ks
            [department_name] => 销售4科室
            [department_phone] => 
            [department_cornet] => 
            [department_description] => 
            [department_sort] => 0
            [is_delete] => 1
            [department_status] => 1
            [add_time] => 1470039208
            [add_user_id] => 1
            [edit_time] => 1470039208
            [edit_user_id] => 1
        )

)

这个还是三级分类有可能搜到的是最后一级 那还要找俩级 怎么去合并啊

只想到用foreach 将相同父id的数据放到同一个以parent_id为key的数组内,如果要父级信息的话还可以进行次逻辑判断

$result =[]

foreach($list as $key => $val){
    if($val['parent_id'] != 0) {
         $result[$val['parent_id']][] = $val;
    }
}

1、增加一个path字段,表示从根节点到叶子节点的路径比如 1.3.7

要求是搜索 然后找出搜索结果的所有父级和子集 树状图显示 关键字标红

我的做法是
1.创建新数组 用来保存所有需要展示的数据的id 初始数据为查询到的id
2.先根据查询id找到所有父级 放在新数组
3.根据id查询所有子集id 这里可能是多个 我是循环 用array_unshift 放进新数组
4.给新数组的id去重(我这里最终组合的是一个一维数组)
5.循环数组id查询所有数据
6.进行分类组合

感谢回答的朋友 有更好的办法请分享出来 哈哈

人气教程排行