Current Location: Home> Latest Articles> How to use array_fill() function? Detailed explanation of its basic grammar and common application scenarios

How to use array_fill() function? Detailed explanation of its basic grammar and common application scenarios

M66 2025-05-14

array_fill() is a very practical function in PHP that allows you to create an array and fill in the specified value. It is widely used to generate arrays of certain duplicate values, or to initialize arrays in certain specific scenarios. This article will introduce in detail the basic syntax and common application scenarios of the array_fill() function.

1. Basic syntax of array_fill() function

 array array_fill ( int $start_index , int $num , mixed $value )
  • $start_index : The starting index position of the fill, specifying where to start the fill.

  • $num : The number of elements to be filled.

  • $value : The value of each element can be any data type, including integers, strings, arrays, etc.

This function returns an array filled with the specified value.

2. Use array_fill() function to fill array

Example 1: Fill in array of numbers

Suppose we want to create an array with 5 elements, each of which is an integer of 10:

 <?php
$filledArray = array_fill(0, 5, 10);
print_r($filledArray);
?>

Output result:

 Array
(
    [0] => 10
    [1] => 10
    [2] => 10
    [3] => 10
    [4] => 10
)

In this example, the array is filled from index 0, with a total of 5 elements, each of which has a value of 10.

Example 2: Filling a string array

You can also use the array_fill() function to fill a string array. For example, create an array with 3 elements, each of which is a string "hello":

 <?php
$filledArray = array_fill(0, 3, "hello");
print_r($filledArray);
?>

Output result:

 Array
(
    [0] => hello
    [1] => hello
    [2] => hello
)

3. Fill the array with negative index

You can also use negative indexes to fill the array. Negative indexes are calculated from the end of the array. For example, create an array that starts with index-3:

 <?php
$filledArray = array_fill(-3, 4, "test");
print_r($filledArray);
?>

Output result:

 Array
(
    [-3] => test
    [-2] => test
    [-1] => test
    [0] => test
)

In this example, the filled index starts with -3 and is filled with 4 elements.

4. Common application scenarios of array_fill()

Scenario 1: Initialize the array

array_fill() can be used to quickly initialize an array containing the same elements, especially if you know the size and initial value of the array. For example, create an array with 100 elements with a value of false to represent a boolean array:

 <?php
$filledArray = array_fill(0, 100, false);
print_r($filledArray);
?>

Scenario 2: Fill in indexes for specific ranges

Sometimes, you may need to populate an array with a specific index range. For example, you want to create an array where the elements from index 2 to index 6 are null :

 <?php
$filledArray = array_fill(2, 5, null);
print_r($filledArray);
?>

Output result:

 Array
(
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
)

In this example, the array is filled from index 2, with a total of 5 elements filled with each element's value null .

Scene 3: Generate an array of duplicate values

array_fill() can also be used to generate duplicate arrays of values. In some cases, it is very common to generate an array containing the same elements. For example, you want to generate an array of 10 "m66.net" strings to simulate a URL list:

 <?php
$urls = array_fill(0, 10, "https://m66.net");
print_r($urls);
?>

Output result:

 Array
(
    [0] => https://m66.net
    [1] => https://m66.net
    [2] => https://m66.net
    [3] => https://m66.net
    [4] => https://m66.net
    [5] => https://m66.net
    [6] => https://m66.net
    [7] => https://m66.net
    [8] => https://m66.net
    [9] => https://m66.net
)

This example shows how to generate an array of multiple URLs, each pointing to m66.net .

5. Things to note

  • array_fill() will generate a new array based on the index range you provide. If the starting index you specify is a negative number, it will be filled from the end of the array.

  • The function does not check if the index has been filled, so if you repeatedly call it with a different index, the final array's index may be overwritten.

Summarize

The array_fill() function is a very simple and powerful tool to generate an array that fills the specified value. It can be used to initialize an array, fill in indexes of a specific range, or generate an array of duplicate values. Understanding and flexibly applying array_fill() can improve your efficiency in processing arrays and play an important role in actual development.