Home > Backend Development > PHP Tutorial > Generate tree structure (such as provinces and cities), tree structure provinces and cities_PHP tutorial

Generate tree structure (such as provinces and cities), tree structure provinces and cities_PHP tutorial

WBOY
Release: 2016-07-12 08:51:46
Original
1763 people have browsed it

Generate tree structure (such as provinces and cities), tree structure provinces and cities

<br /><?<span>php
</span><span>header</span>("Content-type: text/html; charset=utf-8"<span>);

</span><span>function</span> generateTree(<span>$items</span><span>)
{
    </span><span>$tree</span> = <span>array</span><span>();
    </span><span>foreach</span>(<span>$items</span> <span>as</span> <span>$item</span><span>){
        </span><span>if</span>(<span>isset</span>(<span>$items</span>[<span>$item</span>['pid'<span>]])){
            </span><span>$items</span>[<span>$item</span>['pid']]['son'][] = &<span>$items</span>[<span>$item</span>['id'<span>]];
        }</span><span>else</span><span>{
            </span><span>$tree</span>[] = &<span>$items</span>[<span>$item</span>['id'<span>]];
        }
    }
    </span><span>return</span> <span>$tree</span><span>;
}<br />//需要注意的一点,数组的key值必须与id值保持一致
</span><span>$items</span> = <span>array</span><span>(
    </span>1 => <span>array</span>('id' => 1, 'pid' => 0, 'name' => '安徽省'),
    2 => <span>array</span>('id' => 2, 'pid' => 0, 'name' => '浙江省'),
    3 => <span>array</span>('id' => 3, 'pid' => 1, 'name' => '合肥市'),
    4 => <span>array</span>('id' => 4, 'pid' => 3, 'name' => '长丰县'),
    5 => <span>array</span>('id' => 5, 'pid' => 1, 'name' => '安庆市'),<span>
);
</span><span>echo</span> "<pre class="brush:php;toolbar:false">"<span>;
</span><span>print_r</span>(generateTree(<span>$items</span>));
Copy after login

Optimization of the above method:

Generate tree structure (such as provinces and cities), tree structure provinces and cities_PHP tutorialphp header("Content-type: text/html; charset=utf-8"); 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(); } //One thing to note is that the key value of the array must be consistent with the id $items = array( 1 => array('id' => 1, 'pid' => 0, 'name' => 'Anhui Province'), 2 => array('id' => 2, 'pid' => 0, 'name' => 'Zhejiang Province'), 3 => array('id' => 3, 'pid' => 1, 'name' => 'Hefei City'), 4 => array('id' => 4, 'pid' => 3, 'name' => 'Changfeng County'), 5 => array('id' => 5, 'pid' => 1, 'name' => 'Anqing City'), ); echo "
"<span>;
</span><span>print_r</span>(generateTree(<span>$items</span>));
View Code

Result:

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1128373.htmlTechArticleGenerate a tree structure (such as provinces and cities), tree structure provinces and cities? php header ("Content- type: text/html; charset=utf-8" ); function generateTree( $items ){ $tree = array (); foreach ( $...
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