First create a data table with infinite classification. I use the types of id, name, and pid here (of course there are many ways of infinite classification, such as: id, name, pid, path. left , the form of left and right nodes)
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
I will use an array to simulate it here without adding data. . .
$items = array(
1 => array('sid' => 1, 'pid' => 0, 'name' => 'Jiangxi Province'),
2 = > array('sid' => 2, 'pid' => 0, 'name' => 'Heilongjiang Province'),
3 => array('sid' => 3, ' pid' => 1, 'name' => 'Nanchang City'),
4 => array('sid' => 4, 'pid' => 2, 'name' => 'Harbin City'),
5 => array('sid' => 5, 'pid' => 2, 'name' => 'Jixi City'),
6 => array('sid' => 6, 'pid' => 4, 'name' => 'Xiangfang District'),
7 => array('sid' => 7, 'pid ' => 4, 'name' => 'Nangang District'),
8 => array('sid' => 8, 'pid' => 6, 'name' => ' Hexing Road'),
9 => array('sid' => 9, 'pid' => 7, 'name' => 'Xidazhi Street'),
10 = > array('sid' => 10, 'pid' => 8, 'name' => 'Northeast Forestry University'),
11 => array('sid' => 11, 'pid' => 9, 'name' => 'Harbin Institute of Technology'),
12 => array('sid' => 12, 'pid' => 8, 'name' = > 'Harbin Normal University'),
13 => array('sid' => 13, 'pid' => 1, 'name' => 'Ganzhou City'),
14 => array('sid' => 14, 'pid' => 13, 'name' => 'Gan County'),
15 => array('sid' => 15, 'pid' => 13, 'name' => 'Yudu County'),
16 => array('sid' => 16, 'pid' => 14, 'name' = > 'Maodian Town'),
17 => array('sid' => 17, 'pid' => 14, 'name' => 'Dataan Township'),
18 => array('sid' => 18, 'pid' => 16, 'name' => 'Yiyuan Village'),
19 => array('sid' => 19 , 'pid' => 16, 'name' => 'Shangba Village'),
20 => array('sid' => 20, 'pid' => 0, 'name' => 'Guangdong Province'),
21 => array('sid' => 21, 'pid' => 20, 'name' => 'Guangzhou City'),
22 => array('sid' => 22, 'pid' => 20, 'name' => 'Dongguan City'),
);
The code is as follows:
$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 "";
The printed results are as follows:
The above code was collected by me on the Internet. I think it is relatively classic, and compared to others, the amount of code is small. .
Thank you to the great people on the Internet for sharing with us. . .
.