Three-level classification like query queries many IDs and needs to find the corresponding parent ID and combine them based on the parent ID. How should the data be combined based on the parent ID?

WBOY
Release: 2016-08-04 09:19:17
Original
1216 people have browsed it

This is the data obtained. The parent id is 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>
Copy after login
Copy after login

This is still a three-level classification. It is possible that the last level is found. Then there are two more levels. How to merge it?

Reply content:

This is the data obtained. The parent id is 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>
Copy after login
Copy after login
This is still a three-level classification. It is possible that the last level is found. Then there are two more levels. How to merge it?

I only think of using

foreach

to put data with the same parent ID into the same array with parent_id as the key. If you want parent information, you can also make a logical judgment <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">&lt;code&gt;$result =[] foreach($list as $key =&gt; $val){ if($val['parent_id'] != 0) { $result[$val['parent_id']][] = $val; } } &lt;/code&gt;</pre><div class="contentsignin">Copy after login</div></div>

1. Add a path field to represent the path from the root node to the leaf node, such as 1.3.7

The requirement is to search and then find all parents and subsets of the search results. The tree diagram displays the keywords in red

My approach is

1. Create a new array to save the ID of all the data that needs to be displayed. The initial data is the queried ID

2. First find all the parents based on the query ID and put them in the new array
3. Query all based on the ID There may be multiple subset ids here. I loop and use array_unshift to put them into the new array
4. Deduplicate the ids of the new array (the final combination here is a one-dimensional array)
5. Loop the array id to query all data
6 .Category combination

Thanks to those who answered. If you have a better way, please share it, haha

Related labels:
php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template