Current Location: Home> Latest Articles> How to quickly generate an array of specified size and fill default values ​​using PHP's array_fill()?

How to quickly generate an array of specified size and fill default values ​​using PHP's array_fill()?

M66 2025-05-17

How to quickly generate an array of specified size and fill default values ​​using PHP's array_fill()?

In PHP, it is a common requirement to generate an array with a fixed size and fill the default values. Fortunately, PHP provides a very simple and efficient function array_fill() to help us achieve this. This article will explain how to use the array_fill() function to quickly generate an array with a specified size and fill a default value.

1. Introduction to array_fill() function

The array_fill() function is used to create an array of specified size and fill each element with the same value. This function accepts the following three parameters:

 array_fill(int $start_index, int $num, mixed $value) : array
  • $start_index : The starting index of the array, which determines which value the index of the array is generated starts from.

  • $num : The number of array elements to be generated.

  • $value : The default value for each array element.

2. Basic usage

Using array_fill() to create an array with a specified number of elements and all elements are default values ​​is very simple. Here is a basic example:

 <?php
// Create a Container10Elements,All values ​​are0Array of
$array = array_fill(0, 10, 0);

// Output result
print_r($array);
?>

Output result:

 Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
    [4] => 0
    [5] => 0
    [6] => 0
    [7] => 0
    [8] => 0
    [9] => 0
)

In this example, array_fill() starts with index 0 and creates an array of 10 elements, all of which have values ​​0.

3. Use negative indexes

array_fill() can also use negative indexes to create arrays. For example, the following code uses negative indexes to fill an array:

 <?php
// 使用负数索引Create a Container10ElementsArray of
$array = array_fill(-5, 10, "Hello");

// Output result
print_r($array);
?>

Output result:

 Array
(
    [-5] => Hello
    [-4] => Hello
    [-3] => Hello
    [-2] => Hello
    [-1] => Hello
    [0] => Hello
    [1] => Hello
    [2] => Hello
    [3] => Hello
    [4] => Hello
)

In this example, the index of the array starts with -5 and is filled with 10 "Hello" values.

4. Common application scenarios

4.1 Fill in the initial value

array_fill() is very suitable for initializing arrays. For example, suppose you want to create an array of 100 elements with the initial value of all elements false , you can use the following code:

 <?php
$array = array_fill(0, 100, false);

4.2 Create a key-value array

array_fill() can not only be used to fill simple numeric values, but also to fill associative arrays. Suppose we want to create an array with a specific key and fill the same value:

 <?php
$array = array_fill(1, 5, "default_value");
print_r($array);
?>

Output result:

 Array
(
    [1] => default_value
    [2] => default_value
    [3] => default_value
    [4] => default_value
    [5] => default_value
)

In this example, we start with index 1 and create an array of length 5, with all elements having values ​​of "default_value" .

5. Process URL

If your array content involves URLs, then it is very convenient to fill each array element into a fixed URL when generating an array through array_fill() , for example:

 <?php
// Create a Container5ElementsArray of,All elements are specifiedURL
$urls = array_fill(0, 5, "https://m66.net/");

// Output result
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/
)

In this example, we use array_fill() to create an array of 5 elements, each of which is the same URL, and the domain name has been replaced with m66.net .

6. Summary

PHP's array_fill() function is a very powerful tool that allows us to quickly generate arrays of specified sizes and fill default values. Whether creating a simple array of numeric values ​​or generating an associative array with custom key names, array_fill() can be easily handled. At the same time, it can also handle the situation where URLs are included, making it more flexible and convenient when processing URLs in arrays.