-
- $array = array(
- 0=>array("","Hebei"),
- 1=>array("","Beijing"),
- 2=> array(0,"Baoding"),
- 3=>array(1,"Haidian"),
- 4=>array(3,"Zhongguancun"),
- 5=>array(2,"Zhuozhou")
- );
- ?>
Copy the code
and return the following results after processing:
Hebei
-Baoding
--Zhuozhou
Beijing
-Haidian
--Zhongguancun
Algorithm code:
-
- function typeArray($array){
- $con = null;
- foreach ($array as $k=>$v){
- $na[$k] = is_numeric($ v[0]) ? $na[$v[0]].$k."|" : $k."|";
- }
- asort($na); //Sort
- foreach ($na as $k =>$v){
- $s = substr_count($v,"|");
- $con .= str_repeat("-",($s-1)).$array[$k][1]. "n";
- }
- return $con;
- }
- ?>
Copy code
|