PHP regional classification sorting algorithm example code

怪我咯
Release: 2023-03-12 21:44:01
Original
1289 people have browsed it

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; 
    }
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!