search
Homephp教程php手册关于无限分类的树状输出(id,name,pid)类型的

关于无限分类的树状输出(id,name,pid)类型的

Jun 13, 2016 am 11:31 AM
namepidaboutClassificationcreateIdata sheetunlimitedyesuseoftypeoutputfirst

首先创建无限分类的数据表,我这里采用的是id、name、pid这种类型(当然还有很多种无限分类的方式了,比如:id、name、pid、path。left、right左右节点的形式)

CREATE TABLE `class` (
`id` int(10) unsigned NOT NULL auto_increment,
`name` varchar(128) default NULL,
`pid` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8                                                         

我这里就用数组来模拟,不添加数据了。。。

$items = array(
1 => array('sid' => 1, 'pid' => 0, 'name' => '江西省'),
2 => array('sid' => 2, 'pid' => 0, 'name' => '黑龙江省'),
3 => array('sid' => 3, 'pid' => 1, 'name' => '南昌市'),
4 => array('sid' => 4, 'pid' => 2, 'name' => '哈尔滨市'),
5 => array('sid' => 5, 'pid' => 2, 'name' => '鸡西市'),
6 => array('sid' => 6, 'pid' => 4, 'name' => '香坊区'),
7 => array('sid' => 7, 'pid' => 4, 'name' => '南岗区'),
8 => array('sid' => 8, 'pid' => 6, 'name' => '和兴路'),
9 => array('sid' => 9, 'pid' => 7, 'name' => '西大直街'),
10 => array('sid' => 10, 'pid' => 8, 'name' => '东北林业大学'),
11 => array('sid' => 11, 'pid' => 9, 'name' => '哈尔滨工业大学'),
12 => array('sid' => 12, 'pid' => 8, 'name' => '哈尔滨师范大学'),
13 => array('sid' => 13, 'pid' => 1, 'name' => '赣州市'),
14 => array('sid' => 14, 'pid' => 13, 'name' => '赣县'),
15 => array('sid' => 15, 'pid' => 13, 'name' => '于都县'),
16 => array('sid' => 16, 'pid' => 14, 'name' => '茅店镇'),
17 => array('sid' => 17, 'pid' => 14, 'name' => '大田乡'),
18 => array('sid' => 18, 'pid' => 16, 'name' => '义源村'),
19 => array('sid' => 19, 'pid' => 16, 'name' => '上坝村'),
20 => array('sid' => 20, 'pid' => 0, 'name' => '广东省'),
21 => array('sid' => 21, 'pid' => 20, 'name' => '广州市'),
22 => array('sid' => 22, 'pid' => 20, 'name' => '东莞市'),
);

 

代码如下:

 

$t = array();
foreach ($items as $id => $item) {
  echo $item['pid'];
  if ($item['pid']) {
    $items[$item['pid']][$item['sid']] = &$items[$item['sid']];
    $t[] = $id;
  }
}

foreach($t as $u) {
  unset($items[$u]);
}
echo "

";<br>print_r($items);<br>echo "
";

 

打印的结果如下:

 

 

 

 

以上代码是我在网上搜集的,觉得比较经典,而且相对于其他的来说,代码量也少。。

感谢网络中的大神能和我们一起分享。。。

 

 

 

 

 

 

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 Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.