Home > Backend Development > PHP Tutorial > PHP algorithm for classifying and sorting regions

PHP algorithm for classifying and sorting regions

WBOY
Release: 2016-07-25 09:07:41
Original
1017 people have browsed it
  1. $array = array(
  2. 0=>array("","Hebei"),
  3. 1=>array("","Beijing"),
  4. 2=> array(0,"Baoding"),
  5. 3=>array(1,"Haidian"),
  6. 4=>array(3,"Zhongguancun"),
  7. 5=>array(2,"Zhuozhou")
  8. );
  9. ?>
Copy the code

and return the following results after processing: Hebei -Baoding --Zhuozhou Beijing -Haidian --Zhongguancun

Algorithm code:

  1. function typeArray($array){
  2. $con = null;
  3. foreach ($array as $k=>$v){
  4. $na[$k] = is_numeric($ v[0]) ? $na[$v[0]].$k."|" : $k."|";
  5. }
  6. asort($na); //Sort
  7. foreach ($na as $k =>$v){
  8. $s = substr_count($v,"|");
  9. $con .= str_repeat("-",($s-1)).$array[$k][1]. "n";
  10. }
  11. return $con;
  12. }
  13. ?>
Copy code


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