PHP recursively implements infinite classification

不言
Release: 2023-03-23 09:42:01
Original
3842 people have browsed it

The content of this article is PHP recursive implementation of infinite classification. Now I share it with everyone. Friends in need can also refer to it. Let’s come and take a look.

$datasection = array(

    array('id' => 1, 'name' => '安徽', 'pid' => 0),
    array('id' => 2, 'name' => '北京', 'pid' => 0),
    array('id' => 3, 'name' => '海淀', 'pid' => 2),
    array('id' => 4, 'name' => '中关村', 'pid' => 3),
    array('id' => 5, 'name' => '合肥', 'pid' => 1),
    array('id' => 6, 'name' => '上地', 'pid' => 3),
    array('id' => 7, 'name' => '河北', 'pid' => 0),
    array('id' => 8, 'name' => '石家庄', 'pid' => 7),
    
    );

function getTree($data, $pId)
{
	$tree = '';
	foreach($data as $k => $v)
	{
		if($v['pid'] == $pId)
		{
			$v['pid'] = getTree($data, $v['id']);
			$tree[] = $v;
			unset($data[$k]);
		}
	}
	return $tree;
}

$tree = getTree($datasection, 0);
print_r($tree);
Copy after login

Related recommendations:

PHP Infinitus Classification

php Infinite Classification development process and example analysis

The above is the detailed content of PHP recursively implements infinite classification. For more information, please follow other related articles on the PHP Chinese website!

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!