: This article mainly introduces the Infinitus classification of PHP. For more information about PHP tutorials, see. Infinitus classification: recursive and iterative implementation
1. find the subtopic of the specified topic
2. find the child column of the specified topic, that is, the child tree
3. find the parent directory, grandfather Directory,..., top-level Directory of the specified column
Raw data to be processed
$ Area = array ('id' => 1, 'name' => 'Shandong ', 'parent' => 0), array ('id' => 2, 'name' => 'heze ', 'parent' => 1), array ('id' => 3, 'name' => 'dingta ', 'parent' => 2), array ('id' => 4, 'name' => 'Qingdao ', 'parent' => 1 ), array ('id' => 5, 'name' => 'leshi', 'parent' => 4), array ('id' => 6, 'name' => 'yantai ', 'parent' => 1), array ('id' => 7, 'name' => 'nanshan ', 'parent' => 6), array ('id' => 8, 'name' => 'Imitation mountain ', 'parent' => 3 ), array ('id' => 9, 'name' => 'rizhao ', 'parent' => 1), array ('id' => 10, 'name' => 'jinan ', 'parent' => 1 ));
// Find the subtopic function findSon ($ arr, $ id = 0) {// find that the value of the parent of the $ arr element is equal to $ id, which is its subdirectory $ sforeach ($ arr as $ v) {if ($ v ['parent'] ==$ id) {$ sons [] = $ v ;}} return $ sons ;}
// Find the descendant tree
// ExploitationStatic variablesFunction findTree ($ arr, $ id, $ lev= 1) {static $ subs = array (); foreach ($ arr as $ v) {if ($ v ['parent'] ==$ id) {$ v ['lev'] = $ lev; $ subs [] = $ v; findTree ($ arr, $ v ['id'], $ lev+ 1) ;}return $ subs;} method 2, function findTree ($ arr, $ id, $ lev= 1) {static $ subs = array (); foreach ($ arr as $ v) {if ($ v ['parent'] = $ id) {$ v ['lev'] = $ lev; $ subs [] = $ v; $ subs = $ subs + findTree ($ arr, $ v ['id'], $ lev_+ 1) ;}}return $ subs;} method 3. function findTree ($ arr, $ id, $ lev_= 1) {$ subs = array (); foreach ($ arr as $ v) {if ($ v ['parent'] ==$ id) {$ v ['lev'] = $ lev; $ subs [] = $ v; $ subs = array_merge ($ subs, findTree ($ arr, $ v ['id'], $ EV + 1 ));}} return $ subs ;}
// Use the iteration method to find the child tree function subTree ($ arr, $ parent = 0) {$ task = array ($ parent); // task table $ tree = array (); // while (! Empty ($ task) {$ flag = false; foreach ($ arr as $ k = >$ v) {if ($ v ['parent'] = $ parent) {$ tree [] = $ v; array_push ($ task, $ v ['id']); // the latest region id is added to the task stack $ parent = $ v ['id']; unset ($ arr [$ k]); // unset $ flag = true;} if ($ flag = false) {array_pop ($ task); $ parent = end ($ task );}} return $ tree ;}
// ====/// Infinitus classification // genealogy tree // === method 1, function familyTree ($ arr, $ id, $ lev= 1) {// static $ tree = array (); foreach ($ arr as $ v) {if ($ v ['id'] ==$ id) {$ v ['lev'] = $ lev; // Determine whether the subclass has a parent directory. if ($ v ['parent']> 0) {$ tree = array_merge ($ tree, familyTree ($ arr, $ v ['parent'], $ EV + 1); // familyTree ($ arr, $ v ['parent']);} $ tree [] = $ v;} return $ tree;} method 2. // use iterative methods to find the family tree function tree ($ arr, $ id) {$ tree = array (); while ($ id! = 0) {foreach ($ arr as $ v) {if ($ v ['id'] = $ id) {$ tree [] = $ v; $ id = $ v ['parent']; break ;}}return $ tree ;}
The above introduces the PHP Infinitus classification, including static variables, and hopes to help those who are interested in PHP tutorials.