Rumah  >  Artikel  >  pembangunan bahagian belakang  >  PHP无限极分类之数组_PHP教程

PHP无限极分类之数组_PHP教程

WBOY
WBOYasal
2016-07-12 09:03:35876semak imbas

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;
}
 
 
 
?>

PHP无限极分类之数组_PHP教程

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1079403.htmlTechArticlePHP无限极分类之数组 ?php$data = array( 1 = array(id = 1,name = name1,pid = 0), 2 = array(id = 2,name = name2,pid = 0), 3 = array(id = 3,name = name3,pid = 1), 4 = array(id...
Kenyataan:
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