<?php
function matrix($n){
$y = $x = ($n - 1) / 2;
$num = 2;
$total = pow($n, 2);
$arr = array_fill(0, $n, array_fill(0, $n, 1));
$i = 0;
$limit = 1;
while ($num <= $total) {
for ($j = 0; $num <= $total && $j < $limit; ++$j) {
switch ($i) {
case 0 :
++$y;
break;
case 1 :
++$x;
break;
case 2 :
--$y;
break;
case 3 :
--$x;
break;
}
$arr[$x][$y] = $num++;
}
if ($i % 2 == 1) {
++$limit;
}
$i = ($i + 1) % 4;
}
return $arr;
}
$arr = matrix(6);
print_r($arr);











