In PHP programming, we often encounter array operation functions. array_fill() and array_fill_keys() are two very common functions that are used to fill arrays. However, many novice developers often confuse these two functions. They have similar functions but essential differences. Today, we will explore the differences between these two functions and analyze how to use them correctly in actual development.
array_fill()
array_fill() is used to fill an array of specified ranges, and all elements have the same value. The basic usage is as follows:
array_fill(int $start_index, int $num, mixed $value): array
$start_index : fills the starting index of the array.
$num : The number of elements to be filled.
$value : The value of each element in the array.
For example, we can create an array of length 5, with each element's value of 100 :
$array = array_fill(0, 5, 100);
print_r($array);
Output:
Array
(
[0] => 100
[1] => 100
[2] => 100
[3] => 100
[4] => 100
)
array_fill_keys() is to fill an array based on the given key, which sets the same value for each specified key. The basic usage is as follows:
array_fill_keys(array $keys, mixed $value): array
$keys : an array containing keys.
$value : The value corresponding to the given key.
For example, suppose we have an array of key names and a value to fill:
$keys = ['a', 'b', 'c'];
$array = array_fill_keys($keys, 100);
print_r($array);
Output:
Array
(
[a] => 100
[b] => 100
[c] => 100
)
Since these two functions look very similar, many beginners are prone to confuse them. The common point they all are that they can be used to fill arrays and both allow you to set a unified value. The difference is that array_fill() fills elements based on the index of the array, while array_fill_keys() fills values based on the given key name.
array_fill() fills the array according to the index range of the array. It requires a starting index and the number of elements to be filled, returning an array with continuous indexes.
array_fill_keys() fills the new array based on a given key array. Each key corresponds to the same value, and the key name of the returned array is customized.
Therefore, the most fundamental difference is that one is to fill by index, and the other is to fill by key name.
Choose to use these two functions according to different needs:
When you need to fill an array with continuous indexes, use array_fill() . For example, you can use an array when initializing an index to fill a certain number of elements.
Example:
$array = array_fill(0, 3, 'PHP');
When you need to fill an array based on a custom key name, use array_fill_keys() . This situation is often used for initialization of associative arrays.
Example:
$keys = ['name', 'age', 'gender'];
$array = array_fill_keys($keys, 'unknown');
Although the functions of array_fill() and array_fill_keys() involve array filling, their usage scenarios and filling methods are different. By understanding the difference between them, you will be able to flexibly select the appropriate functions to operate the array according to your specific needs.
By using these two functions reasonably, you can greatly improve the efficiency of writing PHP code and avoid confusion in actual development.