Home > Backend Development > PHP Tutorial > PHP recursively implements hierarchical tree expansion, php recursive hierarchical tree_PHP tutorial

PHP recursively implements hierarchical tree expansion, php recursive hierarchical tree_PHP tutorial

WBOY
Release: 2016-07-12 08:55:25
Original
1248 people have browsed it

PHP recursively implements hierarchical tree expansion, PHP recursively implements hierarchical tree

The example in this article shares the main code for PHP recursive implementation of hierarchical tree expansion, for your reference, specifically The content is as follows

Rendering:

Implementation code:

<&#63;php 
  
$db = mysql_connect('localhost', 'root', 'root') or die('Can\'t connect to database'); 
mysql_select_db('test') or die('Can\'t find database : test'); 
$result = mysql_query('select id, fid, name from tree'); 
while($arr = mysql_fetch_array($result)){ 
  $data[] = array( 
    'id' => $arr['id'],  
    'fid' => $arr['fid'], 
    'name' => $arr['name'],  
  ); 
} 
  
// 将数据按照缩进简单排列 见图1 
function data2arr($tree, $rootId = 0, $level = 0) { 
  foreach($tree as $leaf) { 
    if($leaf['fid'] == $rootId) { 
      echo str_repeat('    ', $level) . $leaf['id'] . ' ' . $leaf['name'] . '<br/>'; 
      foreach($tree as $l) { 
        if($l['fid'] == $leaf['id']) { 
          data2arr($tree, $leaf['id'], $level + 1); 
          break; 
        } 
      } 
    } 
  } 
} 
  
data2arr($data); 
echo '<br/>-----------------------------------------------------------------------<br/>'; 
  
// 将数据按照所属关系封装 见图2 
function arr2tree($tree, $rootId = 0) { 
  $return = array(); 
  foreach($tree as $leaf) { 
    if($leaf['fid'] == $rootId) { 
      foreach($tree as $subleaf) { 
        if($subleaf['fid'] == $leaf['id']) { 
          $leaf['children'] = arr2tree($tree, $leaf['id']); 
          break; 
        } 
      } 
      $return[] = $leaf; 
    } 
  } 
  return $return; 
} 
  
$tree = arr2tree($data); 
print_r($tree); 
echo '<br/>-----------------------------------------------------------------------<br/>'; 
  
// 将数据使用HTML再次展现 见图3 
function tree2html($tree) { 
  echo '<ul>'; 
  foreach($tree as $leaf) { 
    echo '<li>' .$leaf['name']; 
    if(! emptyempty($leaf['children'])) tree2html($leaf['children']); 
    echo '</li>'; 
  } 
  echo '</ul>'; 
} 
  
tree2html($tree);
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

Articles you may be interested in:

  • A brief analysis of how to use PHP recursive function return values
  • Detailed introduction to PHP’s unlimited classification and support for outputting tree diagrams
  • php recursively obtains the files in the directory (including subdirectories) encapsulation class sharing
  • Notes on using return in php recursive functions
  • php recursive usage examples (php recursive functions)
  • How to use php function recursively and the difference between return and echo
  • php infinite level classification, super simple infinite level classification, supports output tree diagram
  • php infinite classification recursive sorting implementation method
  • PHP tree menu code based on recursive implementation
  • Three basic ways for PHP to implement recursion

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1117041.htmlTechArticlePHP recursive implementation of hierarchical tree expansion, php recursive hierarchical tree The example of this article shares with everyone the PHP recursive implementation of hierarchical tree The main code for shape expansion is for your reference. The specific content is as follows...
Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template