search
Homephp教程php手册PHP无限极分类巧用引用生成树

PHP无限极分类巧用引用生成树

Jun 06, 2016 pm 07:53 PM
linuxphpClassificationQuoteunlimitedgenerateEnter

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 首先看代码实现 function generateTree($items){ $tree = array(); foreach($items as $item){ if(isset($items[$item['pid']])){ $items[$item['pid']]['son'][] = $items[$item['id']];

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

  首先看代码实现

  function generateTree($items){

  $tree = array();

  foreach($items as $item){

  if(isset($items[$item['pid']])){

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

  }else{

  $tree[] = &$items[$item['id']];

  }

  }

  return $tree;

  }

  $items = array(

  1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),

  2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),

  3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),

  4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),

  5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),

  );

  print_r(generateTree($items));

  输出结果

  Array

  (

  [0] => Array

  (

  [id] => 1

  [pid] => 0

  [name] => 安徽省

  [son] => Array

  (

  [0] => Array

  (

  [id] => 3

  [pid] => 1

  [name] => 合肥市

  [son] => Array

  (

  [0] => Array

  (

  [id] => 4

  [pid] => 3

  [name] => 长丰县

  )

  )

  )

  [1] => Array

  (

  [id] => 5

  [pid] => 1

  [name] => 安庆市

  )

  )

  )

  [1] => Array

  (

  [id] => 2

  [pid] => 0

  [name] => 浙江省

  )

  )

  果然是厉害,代码简洁精炼,无需递归,执行速度快。这是我偶然在一个网站上看到的,觉得很实用就收藏下来,分享给大家。

  上面生成树方法还可以精简到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();

  }

  上面这种无限极分类数据树形结构化的方法值得借鉴。但是我觉得这段代码实际用途并不明显啊,你想取出格式化的树形数据还是要递归啊:

  /**

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

  */

  $tree = generateTree($items);

  function getTreeData($tree){

  foreach($tree as $t){

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

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

  getTreeData($t['son']);

  }

  }

  }

  getTreeData($tree);

  不明白为什么他还要递归取出,如果把generateTree()的返回值 输出为json给前端不好吗?

PHP无限极分类巧用引用生成树

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools