PHP打印環狀數字圈

如題:
$i=[number];
打印如下的圖形:

1  2  3  4
12 13 14 5
11 16 15 6
10 9  8  7

 

  1. /**   
  2.  * 二維數組排序   
  3.  * @param   array $array   
  4.  */    
  5. function sort_recursive(&$array){    
  6.     foreach($array as $key=>$edge){    
  7.         ksort($edge,true);    
  8.         $array[$key]=$edge;    
  9.     }    
  10.     ksort($array,true);    
  11. }    
  12.     
  13. /**   
  14.  * 生成一圈環狀數字   
  15.  * @param   array $cotainer    二維數組,包含生成的圈   
  16.  * @param   int $start_value    該圈的起始值   
  17.  * @param   int $x    該圈的偏移值   
  18.  * @param   int $grids_edge    每條邊的數字個數   
  19.  */    
  20. function generate(&$container,$start_value,$x,$grids_edge){    
  21.     $rec_times=ceil($grids_edge/2);    
  22.     $center=$grids_edge&1?false:$start_value+4*$rec_times*($rec_times+1);    
  23.     if($center){    
  24.         $index=$x+ceil($grids_edge/2);    
  25.         $container[$index][$index]=$center;    
  26.     }    
  27.     for($i=0;$i<$grids_edge;$i++){    
  28.         $right=$x+$grids_edge;    
  29.         $basevalue=$start_value+$i;    
  30.         $container[$x][$x+$i]=$base_value;    
  31.         $container[$x+$i][$right]=$grids_edge+$base_value;    
  32.         $container[$right][$right-$i]=$grids_edge*2+$base_value;    
  33.         $container[$right-$i][$x]=$grids_edge*3+$base_value;    
  34.     }    
  35. }    
  36.     
  37.     
  38. $container=array();    
  39. $data=array(    
  40.     1 =>array(1,6),    
  41.     25=>array(2,4),    
  42.     41=>array(3,2),    
  43. );    
  44. /*   
  45. $data=array(   
  46.     1 =>array(1,2),   
  47. );   
  48. $data=array(   
  49.     1&

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *