In PHP, the array_fill() function is used to fill an array with the specified range. It can be used to create an array filled with a specific value, or is useful in some array scenarios that require a fixed value. In this article, we will use 10 practical examples to gain an in-depth understanding of the usage of the array_fill() function to help you get started quickly!
The most basic usage is to create an array of specified size and fill in the given value. For example:
<?php
$array = array_fill(0, 5, 'PHP');
print_r($array);
?>
Output:
Array
(
[0] => PHP
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
)
This code creates an array containing 5 'PHP's .
PHP arrays allow negative numbers as indexes. array_fill() also supports this method:
<?php
$array = array_fill(-3, 5, 'PHP');
print_r($array);
?>
Output:
Array
(
[-3] => PHP
[-2] => PHP
[-1] => PHP
[0] => PHP
[1] => PHP
)
In this example, the index of the array starts with -3 and is filled with 5 'PHP' .
Assuming we want to fill an array of fixed lengths, array_fill() can also easily achieve this:
<?php
$array = array_fill(0, 10, 0);
print_r($array);
?>
Output:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)
Here is an array containing 10 0s .
array_fill() can also be used to fill multi-dimensional arrays. For example, we create a two-dimensional array and fill in a value:
<?php
$array = array_fill(0, 3, array_fill(0, 2, 'PHP'));
print_r($array);
?>
Output:
Array
(
[0] => Array
(
[0] => PHP
[1] => PHP
)
[1] => Array
(
[0] => PHP
[1] => PHP
)
[2] => Array
(
[0] => PHP
[1] => PHP
)
)
This example creates a 3x2 array where each element is an array containing 'PHP' .
Suppose we need to create an array containing multiple user default data:
<?php
$userData = array_fill(0, 5, ['name' => 'User', 'age' => 25]);
print_r($userData);
?>
Output:
Array
(
[0] => Array
(
[name] => User
[age] => 25
)
[1] => Array
(
[name] => User
[age] => 25
)
[2] => Array
(
[name] => User
[age] => 25
)
[3] => Array
(
[name] => User
[age] => 25
)
[4] => Array
(
[name] => User
[age] => 25
)
)
We created an array of 5 user information, each user's information is the same.
By default, array_fill() fills the array starting from index 0, but you can also specify a starting index:
<?php
$array = array_fill(2, 5, 'PHP');
print_r($array);
?>
Output:
Array
(
[2] => PHP
[3] => PHP
[4] => PHP
[5] => PHP
[6] => PHP
)
In this example, the index of the array starts at 2.
If you want to create an associative array and fill with the same value, you can use array_fill() :
<?php
$array = array_fill(1, 5, 'PHP');
$array[0] = 'Start'; // Customize the first element
print_r($array);
?>
Output:
Array
(
[0] => Start
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
[5] => PHP
)
Here we create a custom associative array by manually changing the first element.
You can generate array size and padding values dynamically by calculating:
<?php
$n = 5;
$value = 'PHP';
$array = array_fill(0, $n, $value);
print_r($array);
?>
Output:
Array
(
[0] => PHP
[1] => PHP
[2] => PHP
[3] => PHP
[4] => PHP
)
Here, an array of length 5 is dynamically generated through variables, with the value 'PHP' .
array_fill() can not only fill strings, but also fill any data type, such as integers, floating point numbers, etc.:
<?php
$array = array_fill(0, 5, 3.14);
print_r($array);
?>
Output:
Array
(
[0] => 3.14
[1] => 3.14
[2] => 3.14
[3] => 3.14
[4] => 3.14
)
Here is a populated array containing 5 floating numbers 3.14 .
You can use array_fill () to fill the array and perform further processing with functions such as array_map() :
<?php
$array = array_fill(0, 5, 0);
$array = array_map(fn($v) => $v + 5, $array);
print_r($array);
?>
Output:
Array
(
[0] => 5
[1] => 5
[2] => 5
[3] => 5
[4] => 5
)
Here, first fill an array with a value of 0 using array_fill() , and then add 5 to each element using array_map() .
Through these 10 examples, we can see the powerful function of array_fill() in PHP. It can not only be used to fill array values, but also create a variety of complex array structures for you. In your project, you can flexibly apply this function according to your needs.