Maison >php教程 >PHP源码 >数组分类显示

数组分类显示

PHP中文网
PHP中文网original
2016-05-25 17:07:301324parcourir

php代码

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);

/**
 * 无限分类 方式一
 * 字段 id path name
 * 通过添加父类到子类的路径的一个字符串来判断,数据库中常用,通过concat查询排序
 * 输出时通过排序得出结果
 */
$arr1 = array(
    array(&#39;id&#39; => 1, &#39;path&#39; => &#39;0&#39;, &#39;name&#39; => &#39;中国&#39;),
    array(&#39;id&#39; => 2, &#39;path&#39; => &#39;0&#39;, &#39;name&#39; => &#39;美国&#39;),
    array(&#39;id&#39; => 3, &#39;path&#39; => &#39;0&#39;, &#39;name&#39; => &#39;英国&#39;),
    array(&#39;id&#39; => 4, &#39;path&#39; => &#39;0-1&#39;, &#39;name&#39; => &#39;北京市&#39;),
    array(&#39;id&#39; => 5, &#39;path&#39; => &#39;0-1&#39;, &#39;name&#39; => &#39;上海市&#39;),
    array(&#39;id&#39; => 6, &#39;path&#39; => &#39;0-1&#39;, &#39;name&#39; => &#39;天津市&#39;),
    array(&#39;id&#39; => 7, &#39;path&#39; => &#39;0-1-4&#39;, &#39;name&#39; => &#39;东城区&#39;),
    array(&#39;id&#39; => 8, &#39;path&#39; => &#39;0-1-4&#39;, &#39;name&#39; => &#39;西城区&#39;),
    array(&#39;id&#39; => 9, &#39;path&#39; => &#39;0-1-5&#39;, &#39;name&#39; => &#39;黄浦区&#39;),
    array(&#39;id&#39; => 10, &#39;path&#39; => &#39;0-1-5&#39;, &#39;name&#39; => &#39;普陀区&#39;),
    array(&#39;id&#39; => 11, &#39;path&#39; => &#39;0-1-5-9&#39;, &#39;name&#39; => &#39;南京西路&#39;),
    array(&#39;id&#39; => 12, &#39;path&#39; => &#39;0-1-5-9-11&#39;, &#39;name&#39; => &#39;人民公园&#39;),
    array(&#39;id&#39; => 13, &#39;path&#39; => &#39;0-1-5-10&#39;, &#39;name&#39; => &#39;真北路&#39;),
    array(&#39;id&#39; => 14, &#39;path&#39; => &#39;0-1-5-10-13&#39;, &#39;name&#39; => &#39;上海番茄研究院&#39;)
);

//主要合并path和id并排序,最后安装path长度加空格以便区分
function gettree1($arr) {
    foreach ($arr as $k => $v) {
        $arr[$k][&#39;path&#39;] = $arr[$k][&#39;path&#39;].&#39;-&#39;.$arr[$k][&#39;id&#39;];
    }
    usort($arr, &#39;gettree1_usort&#39;);
    return $arr;
}

function gettree1_usort($a, $b) {
    return strcmp($a[&#39;path&#39;], $b[&#39;path&#39;]);
}

echo &#39;arr1:<br/>&#39;;
echo &#39;<select>&#39;;

foreach (gettree1($arr1) as $k => $v) {
    echo &#39;<option>&#39;.str_repeat(&#39;   &#39;, count(explode(&#39;-&#39;, $v[&#39;path&#39;]))-2).$v[&#39;name&#39;].&#39;</option>&#39;;
}
echo &#39;</select>&#39;;

/**
 * 无限分类 方式二
 * 字段 id pid name
 * 通过递归转化树形数组
 * 可通过递归输出树形数组
 */
$arr2 = array(
    array(&#39;id&#39; => 1, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;中国&#39;),
    array(&#39;id&#39; => 2, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;美国&#39;),
    array(&#39;id&#39; => 3, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;英国&#39;),
    array(&#39;id&#39; => 4, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;北京市&#39;),
    array(&#39;id&#39; => 5, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;上海市&#39;),
    array(&#39;id&#39; => 6, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;天津市&#39;),
    array(&#39;id&#39; => 7, &#39;pid&#39; => &#39;4&#39;, &#39;name&#39; => &#39;东城区&#39;),
    array(&#39;id&#39; => 8, &#39;pid&#39; => &#39;4&#39;, &#39;name&#39; => &#39;西城区&#39;),
    array(&#39;id&#39; => 9, &#39;pid&#39; => &#39;5&#39;, &#39;name&#39; => &#39;黄浦区&#39;),
    array(&#39;id&#39; => 10, &#39;pid&#39; => &#39;5&#39;, &#39;name&#39; => &#39;普陀区&#39;),
    array(&#39;id&#39; => 11, &#39;pid&#39; => &#39;9&#39;, &#39;name&#39; => &#39;南京西路&#39;),
    array(&#39;id&#39; => 12, &#39;pid&#39; => &#39;11&#39;, &#39;name&#39; => &#39;人民公园&#39;),
    array(&#39;id&#39; => 13, &#39;pid&#39; => &#39;10&#39;, &#39;name&#39; => &#39;真北路&#39;),
    array(&#39;id&#39; => 14, &#39;pid&#39; => &#39;13&#39;, &#39;name&#39; => &#39;上海番茄研究院&#39;)
);

//获得树形数组
function gettree2($arr, $pid = 0) {
    $tree = array();
    foreach ($arr as $k => $v) {
        if ($v[&#39;pid&#39;] == $pid) {
            $tree[] = $v;
        }
    }
    if (empty($tree)) {
        return null;
    }
    foreach ($tree as $k => $v) {
        $tree[$k][&#39;son&#39;] = gettree2($arr, $v[&#39;id&#39;]);
    }
    return $tree;
}
echo &#39;<pre class="brush:php;toolbar:false">&#39;;
print_r(gettree2($arr2));
echo &#39;<br/>arr2:<br/>&#39;;
echo &#39;<select>&#39;;
inputtree(gettree2($arr2));
echo &#39;</select>&#39;;

/**
 * 无限分类 方式三
 * 字段 id pid name
 * 非递归通过引用传值转化树形数组,此种情况pid必须与子数组索引有对应关系,才可以使用,一般不使用
 * 可通过递归输出树形数组
 */
$arr3 = array(
    array(&#39;id&#39; => 1, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;中国&#39;),
    array(&#39;id&#39; => 2, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;美国&#39;),
    array(&#39;id&#39; => 3, &#39;pid&#39; => &#39;0&#39;, &#39;name&#39; => &#39;英国&#39;),
    array(&#39;id&#39; => 4, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;北京市&#39;),
    array(&#39;id&#39; => 5, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;上海市&#39;),
    array(&#39;id&#39; => 6, &#39;pid&#39; => &#39;1&#39;, &#39;name&#39; => &#39;天津市&#39;),
    array(&#39;id&#39; => 7, &#39;pid&#39; => &#39;4&#39;, &#39;name&#39; => &#39;东城区&#39;),
    array(&#39;id&#39; => 8, &#39;pid&#39; => &#39;4&#39;, &#39;name&#39; => &#39;西城区&#39;),
    array(&#39;id&#39; => 9, &#39;pid&#39; => &#39;5&#39;, &#39;name&#39; => &#39;黄浦区&#39;),
    array(&#39;id&#39; => 10, &#39;pid&#39; => &#39;5&#39;, &#39;name&#39; => &#39;普陀区&#39;),
    array(&#39;id&#39; => 11, &#39;pid&#39; => &#39;9&#39;, &#39;name&#39; => &#39;南京西路&#39;),
    array(&#39;id&#39; => 12, &#39;pid&#39; => &#39;11&#39;, &#39;name&#39; => &#39;人民公园&#39;),
    array(&#39;id&#39; => 13, &#39;pid&#39; => &#39;10&#39;, &#39;name&#39; => &#39;真北路&#39;),
    array(&#39;id&#39; => 14, &#39;pid&#39; => &#39;13&#39;, &#39;name&#39; => &#39;上海番茄研究院&#39;)
);

//获得树形数组
function gettree3($items, $pid = 0) {
    $r = array();
    foreach ($items as $k => $item) {
        if ($item[&#39;pid&#39;] == $pid) {
            $r[] = &$items[$k];
        } else {
            $items[$item[&#39;pid&#39;]-1][&#39;son&#39;][] = &$items[$k]; //可根据关系修改[$item[&#39;pid&#39;]-1]
        }
    }
    return isset($r) ? $r : array();
}

echo &#39;<br/>arr3:<br/>&#39;;
echo &#39;<select>&#39;;
inputtree(gettree3($arr3));
echo &#39;</select>&#39;;


/**
 * 输出树形数组 字段 id pid name
 * @param $arr array 要输出的数组
 * @param $num int 输出文字前的空格倍数
 */
function inputtree($arr, $num = 0) {
    static $i;
    $i = $num;
    foreach ($arr as $v) {
        if (isset($v[&#39;son&#39;])) {
            echo &#39;<option>&#39;.str_repeat(&#39;   &#39;, $i++).$v[&#39;name&#39;].&#39;</option>&#39;;
            inputtree($v[&#39;son&#39;], $i);
        } else {
            echo &#39;<option>&#39;.str_repeat(&#39;   &#39;, $i).$v[&#39;name&#39;].&#39;</option>&#39;;
        }
    }
    $i--;
}


?>
Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn