Home > php教程 > php手册 > PHP无限级分类生成树实例代码

PHP无限级分类生成树实例代码

WBOY
Release: 2016-06-06 19:55:34
Original
1193 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 上面生成树方法还可以精简到5行: 代码示例: ?php function generateTree($items){ foreach($items as $item) $items[$item['pid']]['son'][$item['id']] = $items[$item['id']]; return isset

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

 

  上面生成树方法还可以精简到5行:

  代码示例:

  

  function generateTree($items){

  foreach($items as $item)

  $items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];

  return isset($items[0]['son']) ? $items[0]['son'] : array();

  }

  上面这种无限极分类数据树形结构化的方法值得借鉴。

  感觉这段代码实际用途并不明显啊,想取出格式化的树形数据还是要递归啊:

  代码示例:

  /**

  * 如何取数据格式化的树形数据

  * @site

  */

  $tree = generateTree($items);

  function getTreeData($tree){

  foreach($tree as $t){

  echo $t['name'].'
';

  if(isset($t['son'])){

  getTreeData($t['son']);

  }

  }

  }

  getTreeData($tree);

  [1] [2] 

PHP无限级分类生成树实例代码

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