thinkphp List_to_tree for unlimited class listing all nodes

Source: Internet
Author: User

The List_to_tree is easy to use and can be viewed in detail in the manual. Because I need to list all the nodes at the same time, so I wrote a recursive function, for everyone to refer to.
Public Function Index () {    Load (' extend ');            Load extension method    $Category =d (' Category ');    $list = $Category->order (' sort desc ')->select ();//implement Peer node sort    $list =list_to_tree ($list, ' id ', ' fid '); For detailed parameters See manual    $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;}


/** * Converts the returned data set to tree * @access public * @param array $list the dataset to be converted * @param string $pid the parent tag field * @param string $level    Level tag field * @return array */function list_to_tree ($list, $PK = ' id ', $pid = ' pid ', $child = ' _child ', $root =0) {//Create Tree    $tree = Array ();        if (Is_array ($list)) {//creates an array reference based on the primary key $refer = Array ();        foreach ($list as $key = + $data) {$refer [$data [$PK]] =& $list [$key];            } foreach ($list as $key + $data) {//To determine if there is a parent $parentId = $data [$pid];            if ($root = = $parentId) {$tree [] =& $list [$key];                    }else{if (isset ($refer [$parentId])) {$parent =& $refer [$parentId];                $parent [$child] [] =& $list [$key]; }}}} return $tree;} /** * Sort Query Result set * @access public * @param array $list Query result * @param string $field sorted field name * @param array $sortby sort type * AS C Forward Sort desc reverse SortNat Natural Sort * @return Array */function list_sort_by ($list, $field, $sortby = ' asc ') {if (Is_array ($list)) {$refer = $resu       Ltset = Array ();       foreach ($list as $i = + $data) $refer [$i] = & $data [$field];                Switch ($sortby) {case ' ASC '://forward sort asort ($refer);           Break                Case ' desc '://Reverse sort arsort ($refer);           Break                Case ' NAT '://Natural sort natcasesort ($refer);       Break       } foreach ($refer as $key = + $val) $resultSet [] = & $list [$key];   return $resultSet; } return false;} /** * Search Data list * @access public * @param array $list data list * @param mixed $condition query criteria * Supports array (' name ' = = $value) or Name= $value * @return array */function list_search ($list, $condition) {if (is_string ($condition)) Parse_str ($cond    Ition, $condition);    Returns the 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.