Infinitus classification is a common and essential function, which is available in almost every project. Application scenarios: pull-down lists, tree lists, and other infinitely classified front-end implementations (the front-end framework has generally been implemented, as long as the backend transmits data to the front-end in the specified format) backend implementation... unlimited classification
It is a common and necessary function, which is available in almost every project.
Application scenarios: drop-down lists, tree lists, etc.
Unlimited classification type
Front-end implementation(The front-end framework is generally implemented, as long as the backend transmits data to the front-end in the specified format)
Backend implementation(This implementation is mainly described below)
Unlimited implementation
function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0){ $tree = array(); foreach ($data as $key => $value) { if ($value[$pidFName] == $pid) { $value[$levelFName] = $level; $value[$showFName] = str_repeat(' ', $level) . '|-' . $value[$titleFName]; $tree[] = $value; unset($data[$key]); $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1); if(!empty($tempArr)){ $tree = array_merge($tree, $tempArr); } } } return $tree;}
Note:
1. $ dataAll data sorted by asc
2. $ showFNameDisplay the field name of the name (formatted)
3. $ titleFNameField name of the title (not formatted)
4. $ levelFNameLevel field name
5. $ pidFNameField name of the parent id
6. $ idFNameId field name
/*** Unlimited classification ** @ param Array $ treeList // an Array that receives processed data * @ param Array $ data // The result set obtained in the database * @ param String $ level // format the hierarchical field name * @ param Int $ pid * @ param Int $ count // level of classification */function tree (& $ treeList, & $ data, $ level, $ show_name, $ field_name, $ field_pid = 'pid', $ field_id = 'id', $ pid = 0, $ count = 0) {foreach ($ data as $ key => $ value) {if ($ value [$ field_pid] ==$ pid) {$ value [$ level] = $ count; $ value [$ show_name] = str_repeat ('', $ count ). '| -'. $ value [$ field_name]; $ treeList [] = $ value; unset ($ data [$ key]); tree ($ treeList, $ data, $ level, $ show_name, $ field_name, $ field_pid, $ field_id, $ value [$ field_id], $ count + 1 );}}}
Note:
1. $ dataAll data sorted by asc
2. the returned list of Infinity data is included in $ treeList.
Public function getTree ($ list, $ parent_id, $ level = 0) {// it should be a static local variable, so as to ensure that, in recursive calls, all // getTree methods operate on a Tree space. Static $ tree = array (); // Save the array of the found classes. // traverse all the classes and use parent_id to determine which ones are the foreach ($ list as $ row) we are looking) {// Determine the currently traversed Category $ row and whether it is the subcategory to be searched. if ($ row ['pid '] = $ parent_id) {// locate a category. // save it. where? $ Row ['level'] = $ level; $ tree [] = $ row; // continue searching for the subcategory of the current $ row category $ this-> getTree ($ list, $ row ['id'], $ level + 1 );}} return $ tree ;}
Note:
1. $ listAll data sorted by asc
Unlimited classification
It is a common and necessary function, which is available in almost every project.
Application scenarios: drop-down lists, tree lists, etc.
Unlimited classification type
Front-end implementation(The front-end framework is generally implemented, as long as the backend transmits data to the front-end in the specified format)
Backend implementation(This implementation is mainly described below)
Unlimited implementation
function infiniteSort($data, $showFName, $titleFName, $pidFName = 'pid', $idFName = 'id', $levelFName = 'level', $pid = 0, $level = 0){ $tree = array(); foreach ($data as $key => $value) { if ($value[$pidFName] == $pid) { $value[$levelFName] = $level; $value[$showFName] = str_repeat(' ', $level) . '|-' . $value[$titleFName]; $tree[] = $value; unset($data[$key]); $tempArr = infiniteSort($data, $showFName, $titleFName, $pidFName, $idFName, $level, $value[$idFName], $level + 1); if(!empty($tempArr)){ $tree = array_merge($tree, $tempArr); } } } return $tree;}
Note:
1. $ dataAll data sorted by asc
2. $ showFNameDisplay the field name of the name (formatted)
3. $ titleFNameField name of the title (not formatted)
4. $ levelFNameLevel field name
5. $ pidFNameField name of the parent id
6. $ idFNameId field name
/*** Unlimited classification ** @ param Array $ treeList // an Array that receives processed data * @ param Array $ data // The result set obtained in the database * @ param String $ level // format the hierarchical field name * @ param Int $ pid * @ param Int $ count // level of classification */function tree (& $ treeList, & $ data, $ level, $ show_name, $ field_name, $ field_pid = 'pid', $ field_id = 'id', $ pid = 0, $ count = 0) {foreach ($ data as $ key => $ value) {if ($ value [$ field_pid] ==$ pid) {$ value [$ level] = $ count; $ value [$ show_name] = str_repeat ('', $ count ). '| -'. $ value [$ field_name]; $ treeList [] = $ value; unset ($ data [$ key]); tree ($ treeList, $ data, $ level, $ show_name, $ field_name, $ field_pid, $ field_id, $ value [$ field_id], $ count + 1 );}}}
Note:
1. $ dataAll data sorted by asc
2. the returned list of Infinity data is included in $ treeList.
Public function getTree ($ list, $ parent_id, $ level = 0) {// it should be a static local variable, so as to ensure that, in recursive calls, all // getTree methods operate on a Tree space. Static $ tree = array (); // Save the array of the found classes. // traverse all the classes and use parent_id to determine which ones are the foreach ($ list as $ row) we are looking) {// Determine the currently traversed Category $ row and whether it is the subcategory to be searched. if ($ row ['pid '] = $ parent_id) {// locate a category. // save it. where? $ Row ['level'] = $ level; $ tree [] = $ row; // continue searching for the subcategory of the current $ row category $ this-> getTree ($ list, $ row ['id'], $ level + 1 );}} return $ tree ;}
Note:
1. $ listAll data sorted by asc
For more articles about PHP unlimited classification best practices, refer to PHP Chinese network!