List_to_tree of Thinkphp enables unlimited classification to list all nodes

Source: Internet
Author: User
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 nodes at the same time when using it, I wrote a recursive function for your reference. Publicfunctionindex () {Load (& #39; extend & #39;); Load the list_to_tree extension method Thinkphp 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 ;}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.