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

数组分类显示

PHP中文网
풀어 주다: 2016-05-25 17:07:30
원래의
1330명이 탐색했습니다.

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


?>
로그인 후 복사
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 추천
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿