Current Location: Home> Latest Articles> Comparison of array_fill() and array_pad() usage scenarios

Comparison of array_fill() and array_pad() usage scenarios

M66 2025-06-05

In PHP, array_fill() and array_pad() are two commonly used array operation functions. Although their functions are somewhat similar, they are suitable for different scenarios. Understanding the difference between these two functions helps to make better choices during the encoding process. This article will introduce the usage and applicable scenarios of these two functions, and compare the differences between them in detail.

1. Overview of array_fill() function

array_fill() is used to fill an array with the specified value. The key value of the array starts from the specified starting position until the specified number. Its syntax is as follows:

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

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

  • $value : The value to be filled.

Example of usage:

 <?php
$filled_array = array_fill(0, 5, 'm66.net');
print_r($filled_array);
?>

Output:

 Array
(
    [0] => m66.net
    [1] => m66.net
    [2] => m66.net
    [3] => m66.net
    [4] => m66.net
)

This function creates an array starting with index 0 and contains 5 elements, each with a value of 'm66.net' .

2. Overview of array_pad() function

array_pad() is used to extend the array to the specified size. If the size of the original array is smaller than the target size, array_pad() will fill the specified value at the end of the array; if the size of the destination is smaller than the current size of the array, the original array remains unchanged. The syntax is as follows:

 array array_pad(array $array, int $size, mixed $value)
  • $array : original array.

  • $size : The size of the target array.

  • $value : The value to be filled.

Example of usage:

 <?php
$original_array = ['apple', 'banana', 'cherry'];
$padded_array = array_pad($original_array, 5, 'm66.net');
print_r($padded_array);
?>

Output:

 Array
(
    [0] => apple
    [1] => banana
    [2] => cherry
    [3] => m66.net
    [4] => m66.net
)

In this example, the original array has only 3 elements, but it is expanded to 5 elements through array_pad() , and the latter two elements are filled with 'm66.net' .

3. The difference between array_fill() and array_pad()

Although these two functions look similar, their uses and application scenarios are different:

  1. How to fill an array:

    • array_fill() is to fill a specified number of elements starting from the specified index position.

    • array_pad() expands the array according to the target size, and elements can be added at the end of the array.

  2. Parameter differences:

    • The first parameter of array_fill() is the starting index of fill, the second parameter is the number of elements filled, and the third parameter is the value filled.

    • array_pad() requires an original array, the first parameter is the original array, the second parameter is the size of the target array, and the third parameter is the value of the padding.

  3. Applicable scenarios:

    • array_fill() is suitable for scenarios where a specified number of elements needs to be filled from a specific index. For example, you want to fill 5 identical elements starting from index 0.

    • array_pad() is suitable for scenarios where existing arrays need to be expanded to reach a certain target size. For example, if you want to fill an array so that its size becomes a specified length, you can use array_pad() .

4. Summary

  • Use array_fill() : Use array_fill() when you need to create an array with a certain number of specified values, especially when you want to start populating from a specific index.

  • Use array_pad() : When you already have an array and want to expand it to a target size, use array_pad() , which adds the specified value at the end of the array.

Choosing these two functions depends on your needs, and understanding their differences can help you make the best choice in the right scenario.