Current Location: Home> Latest Articles> Want to master the array_fill() function in PHP? Let’s take a look at these 10 practical examples to help you get started quickly!

Want to master the array_fill() function in PHP? Let’s take a look at these 10 practical examples to help you get started quickly!

M66 2025-06-05

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!

1. Basic usage

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 .

2. Use negative index

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' .

3. Fill an array of specific lengths

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 .

4. Fill multi-dimensional array

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' .

5. Create user data using array_fill()

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.

6. Customize the starting index

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.

7. Use array_fill() in associative array

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.

8. Dynamically generate fill arrays

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' .

9. Populate specific types of data

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 .

10. Fill the array and apply custom logic

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.