Home >php教程 >php手册 >php 地区分类排序算法

php 地区分类排序算法

WBOY
WBOYOriginal
2016-06-06 20:28:431149browse

本篇文章是对使用php实现地区分类排序算法进行了详细的分析介绍,需要的朋友参考下

写一个函数,将数据
$array = array(
    0=>array("","河北"),
    1=>array("","北京"),
    2=>array(0,"保定"),
    3=>array(1,"海淀"),
    4=>array(3,"中关村"),
    5=>array(2,"涿州")
);
处理后返回如下:
河北
-保定
--涿州
北京
-海淀
--中关村

复制代码 代码如下:


function typeArray($array){
        $con = null;
        foreach ($array as $k=>$v){
            $na[$k] = is_numeric($v[0]) ? $na[$v[0]].$k."|" : $k."|";
        }
        asort($na); //排序
        foreach ($na as $k=>$v){
          $s = substr_count($v,"|");
          $con .= str_repeat("-",($s-1)).$array[$k][1]."\n";
        }
        return $con;
    }

,香港服务器,服务器空间,网站空间
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn