首页 > php教程 > php手册 > 正文

PHP无限极分类之数组

WBOY
发布: 2016-06-13 08:49:53
原创
1307 人浏览过

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.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门推荐
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!