During the development of PHP, we often encounter scenarios where we need to create "sparse arrays" - that is, only certain specific indexes in the array have values, and other parts are empty or undefined. In PHP, sparse arrays can be implemented by manual assignment, but some developers have tried to simplify this process using the array_fill() function. So, is array_fill() really suitable for creating sparse arrays? Let’s analyze it on the spot.
array_fill() is a function for quickly filling arrays. The basic syntax is as follows:
array array_fill ( int $start_index , int $count , mixed $value )
$start_index : Start index.
$count : The number of elements to be filled.
$value : The value of each element.
For example:
$result = array_fill(5, 3, 'test');
print_r($result);
The output is as follows:
Array
(
[5] => test
[6] => test
[7] => test
)
As you can see, it fills 3 elements with the value 'test' starting from index 5.
A core feature of sparse arrays is that there may be "holes" between indexes. For example, we might just need to have values on index 3, 15, and 1000 in the array, while the rest remain empty or undefined.
Manually building sparse arrays might look like this:
$data = [];
$data[3] = 'alpha';
$data[15] = 'beta';
$data[1000] = 'gamma';
This approach is very intuitive, and the array itself does not take up too much space, because PHP's array is essentially an ordered map (hash table) and undefined indexes do not really take up memory.
In theory, you can use array_fill() to create a certain continuous sparse segment. For example:
$data = array_fill(1000, 1, 'only_one');
Output:
Array
(
[1000] => only_one
)
This does implement assignment at high-bit indexes, but array_fill() cannot be done at once if you want to create multiple discontinuous index values hop-by (such as setting values for 3, 15, and 1000 at the same time). You must call it multiple times:
$data = array_fill(3, 1, 'alpha') +
array_fill(15, 1, 'beta') +
array_fill(1000, 1, 'gamma');
This will get a sparse array structure you want, but this actually loses the advantage of "efficient batch generation". In contrast, direct manual assignment is more concise:
$data = [
3 => 'alpha',
15 => 'beta',
1000 => 'gamma'
];
The advantage of array_fill() is continuous initialization, for example:
Generate initial values for the table.
Unified initialization of a certain structure (such as 0 padding, boolean padding).
Controls the initial offset cable to cause the start position.
But if you are facing the need for sparse arrays with "discontinuous indexes" or "large intervals", it is not the most suitable tool.
array_fill() is a very useful function for quickly initializing arrays with continuous indexes. But when building a typical "sparse array", it seems to be powerless. If your needs are discontinuous sparse arrays, the method of directly using index assignment is more flexible and controllable.
Of course, if you do have some unified logic to build sparse index arrays, you can also encapsulate a helper function to combine array_fill() implementation. For example:
function sparse_fill(array $indexes, $value) {
$result = [];
foreach ($indexes as $index) {
$result[$index] = $value;
}
return $result;
}
$data = sparse_fill([3, 15, 1000], 'example');
This method is more suitable for large-scale sparse filling needs and is easier to maintain.