Rumah > php教程 > php手册 > teks badan

PHP无限极分类之数组

WBOY
Lepaskan: 2016-06-13 08:49:53
asal
1307 orang telah melayarinya

PHP无限极分类之数组

<?php
$data = array( 1 => array(&#39;id&#39; => 1,&#39;name&#39; => &#39;name1&#39;,&#39;pid&#39; => 0),
            2 => array(&#39;id&#39; => 2,&#39;name&#39; => &#39;name2&#39;,&#39;pid&#39; => 0),
            3 => array(&#39;id&#39; => 3,&#39;name&#39; => &#39;name3&#39;,&#39;pid&#39; => 1),
            4 => array(&#39;id&#39; => 4,&#39;name&#39; => &#39;name4&#39;,&#39;pid&#39; => 0),
            5 => array(&#39;id&#39; => 5,&#39;name&#39; => &#39;name5&#39;,&#39;pid&#39; => 3),
            6 => array(&#39;id&#39; => 6,&#39;name&#39; => &#39;name6&#39;,&#39;pid&#39; => 3),
    );
 $str = getTree(0);
$str = "<select name=&#39;term&#39;>" . $str . "</select>";
echo $str;
function getTree($id,$spac=0)
{
    $data = array( 1 => array(&#39;id&#39; => 1,&#39;name&#39; => &#39;name1&#39;,&#39;pid&#39; => 0),
            2 => array(&#39;id&#39; => 2,&#39;name&#39; => &#39;name2&#39;,&#39;pid&#39; => 0),
            3 => array(&#39;id&#39; => 3,&#39;name&#39; => &#39;name3&#39;,&#39;pid&#39; => 1),
            4 => array(&#39;id&#39; => 4,&#39;name&#39; => &#39;name4&#39;,&#39;pid&#39; => 0),
            5 => array(&#39;id&#39; => 5,&#39;name&#39; => &#39;name5&#39;,&#39;pid&#39; => 3),
            6 => array(&#39;id&#39; => 6,&#39;name&#39; => &#39;name6&#39;,&#39;pid&#39; => 3),
    );
    $spac = $spac + 2;
    static $str;
     
    foreach ($data as $k => $v) {
        if ($v[&#39;pid&#39;] == $id) {
            $arr[] = $v;
            $str .= "<option value=&#39;" . $v[&#39;id&#39;] . "&#39;>" . str_repeat(" ",$spac) . "|--" . $v[&#39;name&#39;] . "</option>";
            getTree($v[&#39;id&#39;],$spac);
 
        }
 
    }
     
    return $str;
}
 
 
 
?>
Salin selepas log masuk

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!