Home >Backend Development >PHP Tutorial >PHP recursively implements infinite classification

PHP recursively implements infinite classification

不言
不言Original
2018-05-15 10:39:543885browse

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);

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!

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
Previous article:$PHP_THREENext article:$PHP_THREE