首頁  >  文章  >  後端開發  >  三級分類 like查詢 查詢到很多id 需要找到對應的父級id 並根據父級id組合 應該怎麼根據父級id將資料組合在一起呢?

三級分類 like查詢 查詢到很多id 需要找到對應的父級id 並根據父級id組合 應該怎麼根據父級id將資料組合在一起呢?

WBOY
WBOY原創
2016-08-04 09:19:171215瀏覽

這是取得到的資料 父級id為parent_id

<code>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
        )

)</code>

這個還是三級分類有可能搜到的是最後一級 那還要找兩個等級 怎麼去合併啊

回覆內容:

這是取得到的資料 父級id為parent_id

<code>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
        )

)</code>

這個還是三級分類有可能搜到的是最後一級 那還要找兩個等級 怎麼去合併啊

只想到用foreach 將相同父id的資料放到同一個以parent_id為key的數組內,如果要父級資訊的話還可以進行次邏輯判斷

<code>$result =[]

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

1、增加一個path字段,表示從根節點到葉子節點的路徑例如 1.3.7

要求是搜尋 然後找出搜尋結果的所有父級和子集 樹狀圖顯示 關鍵字標紅

我的做法是
1.創建新數組用來保存所有需要展示的數據的id 初始數據為查詢到的id
2.先根據查詢id找到所有父級放在新數組
3.根據id查詢所有子集id 這裡可能是多個我是循環用array_unshift 放進新數組
4.給新數組的id去重(我這裡最終組合的是一個一維數組)
5.循環數組id查詢所有數據
6 .進行分類組合

感謝回答的朋友 有更好的辦法請分享出來 哈哈

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn