How to Use PHP's array_fill() Function to Fill Arrays
This article explains the PHP array_fill() function, its signature, parameter meanings, return value, practical code examples for numeric and alphabetic keys, and important usage considerations for efficiently populating arrays without manual loops.
In PHP's function library, the array_fill() function is a very useful utility for filling a specified number of elements in an array while allowing you to define the keys and values. This article introduces how to use array_fill() .
Function Signature
<code>array_fill(int $start_index, int $num, mixed $value): array</code>Parameter Explanation:
$start_index : the starting index of the array, which must be a non‑negative integer.
$num : the number of elements to fill, which must also be a non‑negative integer.
$value : the value to fill, which can be any scalar or compound type but must be a scalar value.
Return Value Explanation:
The function returns an array containing the elements filled within the specified range.
Usage Example
Below is a sample code that uses array_fill() to create an array of ten elements, each set to 1:
<code><?php
// Fill an array with 10 elements, each value is 1
$arr = array_fill(0, 10, 1);
print_r($arr);
?></code>Output:
<code>Array
(
[0] => 1
[1] => 1
[2] => 1
[3] => 1
[4] => 1
[5] => 1
[6] => 1
[7] => 1
[8] => 1
[9] => 1
)</code>In this example we created an array, used array_fill() to populate ten elements with the value 1, starting at index 0. Because no explicit keys were provided, numeric indexes are used by default.
We can also specify non‑numeric keys, as shown in the following example:
<code><?php
// Use letters as keys to fill the array
$arr = array_fill('a', 5, 'hello');
print_r($arr);
?></code>Output:
<code>Array
(
[a] => hello
[b] => hello
[c] => hello
[d] => hello
[e] => hello
)</code>Here we used letters as keys, filling five elements each with the string 'hello'.
Usage Notes
1. $start_index must be a non‑negative integer; otherwise a warning is issued.
2. $num must be a non‑negative integer; otherwise a warning is issued.
3. $value can be any basic or compound type but must be a scalar; otherwise a warning is issued.
4. When using letters as keys, ensure the alphabetical order is correct to avoid misaligned array entries.
5. If the number of elements to fill is 0, the function returns an empty array.
6. Filling a very large number of elements may consume significant memory and waste system resources.
Summary
The array_fill() function is a highly useful tool for quickly populating arrays, eliminating the need for manual loops. Its usage is straightforward: specify the starting index, the number of elements, and the fill value, while paying attention to parameter correctness to avoid runtime warnings.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.