Home  >  Article  >  Backend Development  >  PHP regional classification sorting algorithm example code

PHP regional classification sorting algorithm example code

怪我咯
怪我咯Original
2017-07-06 10:55:231290browse

This article is a detailed analysis and introduction to the implementation of regional classification sorting algorithm using PHP. Friends who need it can refer to

Write a function to convert the data
$array = array(
0=>array("","Hebei"),
1=>array("","Beijing"),
2=> array(0,"Baoding"),
3=>array(1,"Haidian"),
4=>array(3,"Zhongguancun"),
5=>array( 2,"Zhuozhou")
);
After processing, the return is as follows:
Hebei
-Baoding
--Zhuozhou
Beijing
-Haidian
--Zhongguancun

The code is as follows:

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


The above is the detailed content of PHP regional classification sorting algorithm example code. For more information, please follow other related articles on the PHP Chinese website!

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