Heim > Artikel > Backend-Entwicklung > Eine dreistufige Klassifizierung wie eine Abfrage fragt viele IDs ab und muss die entsprechende übergeordnete ID finden und diese basierend auf der übergeordneten ID kombinieren. Wie sollen die Daten basierend auf der übergeordneten ID kombiniert werden?
Dies sind die erhaltenen Daten. Die Eltern-ID ist 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>
Dies ist immer noch eine dreistufige Klassifizierung. Es ist möglich, dass es zwei weitere Ebenen gibt
Antwortinhalt:
<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>Dies ist immer noch eine dreistufige Klassifizierung. Es ist möglich, dass es zwei weitere Ebenen gibt
Ich denke nur daran,
zu verwenden, um Daten mit derselben übergeordneten ID in dasselbe Array mit parent_id als Schlüssel einzufügen. Wenn Sie übergeordnete Informationen benötigen, können Sie auch eine sekundäre logische Beurteilung vornehmen
foreach
<code>$result =[] foreach($list as $key => $val){ if($val['parent_id'] != 0) { $result[$val['parent_id']][] = $val; } } </code>1. Fügen Sie ein Pfadfeld hinzu, um den Pfad vom Wurzelknoten zum Blattknoten darzustellen, z. B. 1.3.7
Die Anforderung besteht darin, alle übergeordneten Elemente und Teilmengen der Suchergebnisse zu suchen und dann zu finden. Das Baumdiagramm zeigt die Schlüsselwörter in Rot an
Mein Ansatz ist
1. Erstellen Sie ein neues Array, um die IDs aller anzuzeigenden Daten zu speichern.Suchen Sie zunächst alle Eltern entsprechend der Abfrage ID und füge sie in das neue Array ein
3. Fragen Sie alle Teilmengen-IDs basierend auf der ID ab. Es können mehrere IDs vorhanden sein. Ich verwende array_unshift, um sie in ein neues Array einzufügen
4 des neuen Arrays (was ich hier schließlich kombiniert habe, ist ein eindimensionales Array)
Array-ID schleifen, um alle Daten abzufragen
6
Danke für die Antwort, wenn du einen besseren Weg hast, teile ihn bitte.