Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $arr = array();
- $n = 5;
- $row = $n - 1;
- $col = 0;
- $row2;
- $count = 1;
- for (;$row >=0; $row--)
- {
- $row2 = $row;
- $col = 0;
- while ($row2 <= $n - 1 && $col <= $n - 1)
- {
- $arr[$row2][$col] = $count;
- if($count == $n)
- {
- $arr[$row2][$col] = $n + 2;
- }
- elseif($count == $n+1)
- {
- $arr[$row2][$col] = $n + 1;
- }
- elseif($count == $n+2)
- {
- $arr[$row2][$col] = $n;
- }
- $row2++;
- $col++;
- $count++;
- }
- }
- $row = $n - 2;
- $col = $n - 1;
- $col2;
- for (;$row >=0 ; $row--)
- {
- $col2 = $col;
- $row2 = $row;
- while ($row2 >=0)
- {
- if ($count == $n*$n-2)
- {
- $arr[$row2][$col2] = $n*$n-1;
- }
- elseif ($count == $n*$n-1)
- {
- $arr[$row2][$col2] = $n*$n-2;
- }
- else
- {
- $arr[$row2][$col2] = $count;
- }
- $row2--;
- $col2--;
- $count++;
- }
- }
- echo "<table border='1' style='border-collapse:collapse;text-align:center;'>";
- for ($i = 0; $i<$n; $i++)
- {echo "<tr>";
- for($j = 0;$j<$n;$j++)
- {
- echo "<td style='width:40px;height:40px;padding:10px;'>";
- echo $arr[$i][$j];
- echo "</td>";
- }
- }echo "<tr>";
- echo "</table>";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement