<?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);
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号