When developing games, drawing maps, designing tables, or managing coordinate data, we often need to build a two-dimensional array (matrix) structure. PHP provides a very convenient function - array_fill() , which not only quickly fills a one-dimensional array, but also helps us easily build an initialized two-dimensional board or map structure.
This article will introduce the basic usage of array_fill() and demonstrate how to use it to build a regular two-dimensional grid structure, such as an 8x8 chess board.
array_fill() is one of the built-in functions in PHP, and its syntax is as follows:
array_fill(int $start_index, int $count, mixed $value): array
$start_index : Start index.
$count : The number of elements to be filled.
$value : The value of each element.
Example:
$line = array_fill(0, 5, 0);
// result: [0, 0, 0, 0, 0]
To build a two-dimensional array, such as an 8x8 board or map, we can use array_fill() to fill each row in a loop.
<?php
$rows = 8;
$cols = 8;
$board = [];
for ($i = 0; $i < $rows; $i++) {
$board[$i] = array_fill(0, $cols, 0);
}
// 打印result查看结构
echo '<pre>';
print_r($board);
echo '</pre>';
The output $board is a 2D array of 8 rows and 8 columns, and all grids are initialized to 0. You can replace 0 with other tags as needed, such as "empty" , null , or false .
For example, we want to build a 10x10 map, with the default terrain as "grass" :
<?php
$width = 10;
$height = 10;
$map = [];
for ($y = 0; $y < $height; $y++) {
$map[$y] = array_fill(0, $width, 'grass');
}
// Suppose that some coordinates are to be set "water"
$map[2][3] = 'water';
$map[4][7] = 'water';
$map[5][5] = 'mountain';
// Show map array
echo '<pre>';
print_r($map);
echo '</pre>';
This method can be used to build a map with an initial terrain, which is convenient for subsequent modification or rendering of the map interface.
If you want to provide the corresponding data interface address for each grid, for example, click to jump to the grid details page, you can do this:
<?php
$rows = 5;
$cols = 5;
$grid = [];
for ($i = 0; $i < $rows; $i++) {
$grid[$i] = [];
for ($j = 0; $j < $cols; $j++) {
$grid[$i][$j] = "https://m66.net/grid.php?x={$i}&y={$j}";
}
}
echo '<pre>';
print_r($grid);
echo '</pre>';
In this way, each grid corresponds to a specific URL, such as https://m66.net/grid.php?x=2&y=3 , which is very suitable for making a click-and-jump interface.
Through this article you can learn:
How to use array_fill() to quickly initialize a one-dimensional array.
How to build a regular two-dimensional array structure through looping with array_fill() .
How to fill more structured or dynamic content (such as maps, links, etc.) based on coordinates.
This method is simple and efficient, and is an ideal way to build two-dimensional structures such as maps, chessboards, tables, etc.
If you want to further beautify the display, add styles or interact dynamically with JavaScript in actual projects, you can pass the generated data to the front-end for rendering to achieve more advanced effects.