List_to_tree of Thinkphp allows you to list all nodes in an infinite number of categories. The list_to_tree function of Thinkphp is convenient to list all nodes in an infinitely classified manner. For more information, see the manual. Because I need to list all the nodes at the same time when using Thinkphp's list_to_tree to list all nodes in an infinitely classified manner.
List_to_tree is very convenient to use. For more information, see the manual. Because I need to list all nodes at the same time when using it, I wrote a recursive function for your reference.
Public function index () {Load ('extend'); // Load the extension method $ Category = D ('category '); $ list = $ Category-> order ('sort desc')-> select (); // sort nodes of the same level. $ list = list_to_tree ($ list, 'id ', 'fid'); // for detailed parameters, see $ list = $ this-> findChild ($ list); dump ($ list);} protected function findChild ($ arr) {static $ tree = array (); foreach ($ arr as $ key => $ val) {$ tree [] = $ val; if (isset ($ val ['_ child']) {$ this-> findChild ($ val ['_ child']) ;}} return $ tree ;}
/*** Convert the returned dataset to Tree * @ access public * @ param array $ list the dataset to be converted * @ param string $ pid parent tag field * @ param string $ level mark the field * @ return array */function list_to_tree ($ list, $ pk = 'id', $ pid = 'pid', $ child = '_ child', $ root = 0) {// create Tree $ tree = array (); if (is_array ($ list) {// create a primary key-based array reference $ refer = array (); foreach ($ list as $ key => $ data) {$ refer [$ data [$ pk] = & $ list [$ key];} foreach ($ list as $ key => $ data) {// Determine whether parent $ parentId = $ data [$ pid] exists; if ($ root = $ parentId) {$ tree [] = & $ list [$ key];} else {if (isset ($ refer [$ parentId]) {$ parent = & $ refer [$ parentId]; $ parent [$ child] [] = & $ list [$ key] ;}}} return $ tree ;} /*** sort the query result set * @ access public * @ param array $ list query result * @ param string $ field name * @ param array $ sortby sorting type * asc forward sorting desc reverse sorting nat natural sorting * @ return array */function list_sort_by ($ list, $ field, $ sortby = 'asc ') {if (is_array ($ list) {$ refer = $ resultSet = array (); foreach ($ list as $ I => $ data) $ refer [$ I] = & $ data [$ field]; switch ($ sortby) {case 'asc ': // asort ($ refer); break; case 'desc': // arsort ($ refer); break; case 'Nat ': // natural sorting natcasesort ($ refer); break;} foreach ($ refer as $ key => $ val) $ resultSet [] = & $ list [$ key]; return $ resultSet;} return false ;} /*** search for * @ access public * @ param array $ list data list * @ param mixed $ condition query condition * in the data list. array ('name' => $ value) or name = $ value * @ return array */function list_search ($ list, $ condition) {if (is_string ($ condition) parse_str ($ condition, $ condition ); // returned result set $ resultSet = array (); foreach ($ list as $ key => $ data) {$ find = false; foreach ($ condition as $ field =>$ value) {if (isset ($ data [$ field]) {if (0 === strpos ($ value, '/') {$ find = preg_match ($ value, $ data [$ field]);} elseif ($ data [$ field] = $ value) {$ find = true ;}}if ($ find) $ resultSet [] = & $ list [$ key] ;}return $ resultSet ;}
It is very convenient to use list_to_tree to list all nodes in an infinitely classified manner. for details, refer to the manual. Because I need to list all nodes at the same time when using them...