> 백엔드 개발 > PHP 튜토리얼 > 쿼리와 같은 3단계 분류는 여러 ID를 쿼리하고 해당 상위 ID를 찾아 상위 ID를 기준으로 결합해야 합니다.

쿼리와 같은 3단계 분류는 여러 ID를 쿼리하고 해당 상위 ID를 찾아 상위 ID를 기준으로 결합해야 합니다.

WBOY
풀어 주다: 2016-08-04 09:19:17
원래의
1230명이 탐색했습니다.

부모 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>
로그인 후 복사
로그인 후 복사

이것은 여전히 ​​3단계 분류입니다. 그러면 마지막 단계가 발견될 가능성이 있습니다.

답글 내용:

부모 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>
로그인 후 복사
로그인 후 복사

이것은 여전히 ​​3단계 분류입니다. 그러면 마지막 단계가 더 발견될 수 있습니다.

부모 ID가 동일한 데이터를 parent_id를 키로 사용하여 동일한 배열에 넣는 데 foreach를 사용하는 것만 생각합니다.

<code>$result =[]

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

1. 루트 노드에서 리프 노드까지의 경로를 나타내는 경로 필드를 추가합니다(예: 1.3.7

).

요구 사항은 검색 결과의 모든 상위 및 하위 집합을 검색한 다음 찾는 것입니다. 트리 다이어그램에는 키워드가 빨간색으로 표시됩니다.

내 접근 방식은
1. 표시해야 하는 모든 데이터의 ID를 저장하는 새 배열을 만듭니다.
먼저 쿼리에 따라 모든 부모를 찾습니다. ID를 지정하고 새 배열에 넣습니다
3. ID를 기준으로 모든 하위 집합 ID를 쿼리합니다. 여기에는 여러 개의 ID가 있을 수 있습니다. array_unshift를 사용하여 새 배열에 넣습니다.
4. 새로운 배열 (여기서 최종적으로 합친 것이 1차원 배열입니다)
5. 모든 데이터를 조회하기 위한 루프 배열 ID
6. 분류 및 결합

답변 감사합니다. 더 좋은 방법이 있으면 공유해 주세요

관련 라벨:
php
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿