Current Location: Home> Latest Articles> What is the difference between array_fill() and array()?

What is the difference between array_fill() and array()?

M66 2025-06-06

array() is the most common method used in PHP to create arrays. It can be used to create an array containing elements of any type, and the array elements can be defined by specifying the key-value pairs of the array. When creating an array, the keys and values ​​of elements can be set flexibly. The most common ways to use are as follows:

 $array = array(1, 2, 3, 4, 5);
print_r($array);

The above code will create an array containing the numbers 1 to 5. array() allows you to specify specific key values, or omit it. PHP will automatically assign you the default consecutive key names (starting from 0).

 $array = array(
    "name" => "Alice",
    "age" => 25
);
print_r($array);

This code creates an associative array containing "name" and "age" as keys, corresponding to "Alice" and 25, respectively.

2. Introduction to array_fill() function

array_fill() is used to create an array that fills the specified number and element values. Its purpose is to create an array filled with the same value starting from the specified starting index position. array_fill() mainly has the following usage methods:

 $array = array_fill(0, 5, "PHP");
print_r($array);

The code creates an array starting with index 0 , the size of the array is 5 , and the value of each element is the string "PHP" . At this time, the content of the $array array will be:

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

The array_fill() function accepts three parameters:

  1. Start index : The index of the first element of the array.

  2. Quantity : The size of the array.

  3. Value : The value of each array element.

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

  • Different uses :

    • array() can be used to create arrays of any type, whether it is a normal index array or an associative array with a specific key name. It is created in a flexible way, and elements can be defined according to actual needs.

    • array_fill() is mainly used to create an array with the same value, suitable for scenarios where the same value needs to be repeatedly filled. Its parameters are fixed, simple and efficient.

  • How to create an array :

    • array() allows you to manually specify each element of an array, and can even be created by mixing different types (such as index arrays and associative arrays).

    • Each element in the array created by array_fill() is the same, only the length and starting index are customizable.

  • Use scenarios :

    • When creating an array using array() , it is suitable for situations where you need to explicitly specify the values ​​or key values ​​of each element of the array, for example, creating an array with a specific value or key.

    • When creating an array using array_fill() , it is suitable for filling a fixed value scenario, such as initializing an array with the same value.

4. Example comparison

Suppose you need to create an array of length 10, with all elements having values ​​"m66.net". You can do this by array() or array_fill() , but the two are different:

Use array() :

 $array = array_fill(0, 10, "m66.net");
print_r($array);

Use array() to define manually:

 $array = array("m66.net", "m66.net", "m66.net", "m66.net", "m66.net", "m66.net", "m66.net", "m66.net", "m66.net", "m66.net");
print_r($array);

Both methods generate arrays of length 10, each element is "m66.net", but array_fill() is more concise and efficient, especially when dealing with large amounts of the same data.

5. Summary

  • array() is suitable for creating an array of custom content that can handle different data types and key values ​​flexibly.

  • array_fill() is suitable for easy and efficient operation when you need to create an array and fill the same element values.

Which function to choose depends on your specific needs. If you need a complex array containing different elements, array() is undoubtedly the best choice; if you need an array with the same value, array_fill() will make your code more concise and efficient.