<br><span style="color: #000000;">php </span><span style="color: #008080;">header</span>("Content-type: text/html; charset=utf-8"<span style="color: #000000;">); </span><span style="color: #0000ff;">function</span> generateTree(<span style="color: #800080;">$items</span><span style="color: #000000;">) { </span><span style="color: #800080;">$tree</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">(); </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid'<span style="color: #000000;">]])){ </span><span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid']]['son'][] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]]; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$tree</span>[] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]]; } } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$tree</span><span style="color: #000000;">; }<br>//需要注意的一点,数组的key值必须与id值保持一致 </span><span style="color: #800080;">$items</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span>1 => <span style="color: #0000ff;">array</span>('id' => 1, 'pid' => 0, 'name' => '安徽省'), 2 => <span style="color: #0000ff;">array</span>('id' => 2, 'pid' => 0, 'name' => '浙江省'), 3 => <span style="color: #0000ff;">array</span>('id' => 3, 'pid' => 1, 'name' => '合肥市'), 4 => <span style="color: #0000ff;">array</span>('id' => 4, 'pid' => 3, 'name' => '长丰县'), 5 => <span style="color: #0000ff;">array</span>('id' => 5, 'pid' => 1, 'name' => '安庆市'),<span style="color: #000000;"> ); </span><span style="color: #0000ff;">echo</span> "<pre class="brush:php;toolbar:false">"<span style="color: #000000;">; </span><span style="color: #008080;">print_r</span>(generateTree(<span style="color: #800080;">$items</span>));
上面方法的优化:


<span style="color: #000000;">php </span><span style="color: #008080;">header</span>("Content-type: text/html; charset=utf-8"<span style="color: #000000;">); </span><span style="color: #0000ff;">function</span> generateTree(<span style="color: #800080;">$items</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">foreach</span>(<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$item</span><span style="color: #000000;">) </span><span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['pid']]['son'][<span style="color: #800080;">$item</span>['id']] = &<span style="color: #800080;">$items</span>[<span style="color: #800080;">$item</span>['id'<span style="color: #000000;">]]; </span><span style="color: #0000ff;">return</span> <span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$items</span>[0]['son']) ? <span style="color: #800080;">$items</span>[0]['son'] : <span style="color: #0000ff;">array</span><span style="color: #000000;">(); } </span><span style="color: #008000;">//</span><span style="color: #008000;">需要注意的一点,数组的key值必须与id保持一致</span> <span style="color: #800080;">$items</span> = <span style="color: #0000ff;">array</span><span style="color: #000000;">( </span>1 => <span style="color: #0000ff;">array</span>('id' => 1, 'pid' => 0, 'name' => '安徽省'), 2 => <span style="color: #0000ff;">array</span>('id' => 2, 'pid' => 0, 'name' => '浙江省'), 3 => <span style="color: #0000ff;">array</span>('id' => 3, 'pid' => 1, 'name' => '合肥市'), 4 => <span style="color: #0000ff;">array</span>('id' => 4, 'pid' => 3, 'name' => '长丰县'), 5 => <span style="color: #0000ff;">array</span>('id' => 5, 'pid' => 1, 'name' => '安庆市'),<span style="color: #000000;"> ); </span><span style="color: #0000ff;">echo</span> "<pre class="brush:php;toolbar:false">"<span style="color: #000000;">; </span><span style="color: #008080;">print_r</span>(generateTree(<span style="color: #800080;">$items</span>));
结果:
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
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
How Long Does It Take To Beat Split Fiction?
4 weeks agoByDDD
R.E.P.O. Save File Location: Where Is It & How to Protect It?
4 weeks agoByDDD

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

WebStorm Mac version
Useful JavaScript development tools

Notepad++7.3.1
Easy-to-use and free code editor
