Home > php教程 > php手册 > body text

将返回的数据集转换成树

WBOY
Release: 2016-06-07 11:45:44
Original
1308 people have browsed it

/**<br>  * 将返回的数据集转换成树<br>  * @param  array   $list  数据集<br>  * @param  string  $pk    主键<br>  * @param  string  $pid   父节点名称<br>  * @param  string  $child 子节点名称<br>  * @param  integer $root  根节点ID<br>  * @return array          转换后的树<br>  */<br> function list_to_tree($list, $pk = 'id', $pid = 'pid', $child = '_child', $root=0) {<br>     $tree = array();// 创建Tree<br>     if(is_array($list)) {<br>         // 创建基于主键的数组引用<br>         $refer = array();<br>         foreach ($list as $key => $data) {<br>             $refer[$data[$pk]] =& $list[$key];<br>         }<br>         <br>         foreach ($list as $key => $data) {<br>             // 判断是否存在parent<br>             $parentId = $data[$pid];<br>             if ($root == $parentId) {<br>                 $tree[$data[$pk]] =& $list[$key];<br>             }else{<br>                 if (isset($refer[$parentId])) {<br>                     $parent =& $refer[$parentId];<br>                     $parent[$child][] =& $list[$key];<br>                 }<br>             }<br>         }<br>     }<br>     return $tree;<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!