In PHP, array_fill() and range() are two very commonly used functions, respectively, used to generate arrays with specified elements and generate arrays with ordered ranges. They can be used to easily create an ordered array of key-values that can provide great convenience in many programming tasks. Next, we will explain in detail how to use these two functions to create an ordered array of key-values.
The array_fill() function is used to fill in the array with specified elements. It requires three parameters:
start_index : The index at which the array starts.
num : The number of elements in the array.
value : The value of fill.
This function generates an array with a specified number, each element is the same, and starts with the given starting index.
$start_index = 1;
$num = 5;
$value = 'm66.net'; // Fill in content
$array = array_fill($start_index, $num, $value);
print_r($array);
Output :
Array
(
[1] => m66.net
[2] => m66.net
[3] => m66.net
[4] => m66.net
[5] => m66.net
)
In this example, we create an array with indexes starting from 1 via array_fill() . The array contains 5 elements, each with a value of 'm66.net' .
The range() function is used to generate an array containing continuous elements. It accepts two required parameters:
start : The starting value of the array.
end : The end value of the array. In addition, it can also accept a third parameter to specify the step size (the default step size is 1).
$start = 1;
$end = 5;
$array = range($start, $end);
print_r($array);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
)
In this example, the range() function generates an array from 1 to 5 , the elements of the array are incremented from 1 , and the default step size is 1.
Now, we use the array_fill() and range() functions to create an ordered array of key values. You can use range() to generate key names, then use array_fill() to fill in the values for each key, and finally get an ordered array of key values.
$keys = range(1, 5); // Generate key names:1, 2, 3, 4, 5
$values = array_fill(0, count($keys), 'm66.net'); // Fill in the same value
$assoc_array = array_combine($keys, $values); // Merge into key-value pairs
print_r($assoc_array);
Output :
Array
(
[1] => m66.net
[2] => m66.net
[3] => m66.net
[4] => m66.net
[5] => m66.net
)
In this example, we first create an array containing numbers 1 to 5 as the key name using range() . Then, we use array_fill() to create an array containing the same value 'm66.net' , and finally merge the key names and fill values into an ordered array of key values through array_combine() .
By combining array_fill() and range() , we are able to create an ordered array of key values quickly and efficiently. This approach is especially suitable for scenarios where key names and corresponding values are needed to quickly generate, avoiding manual writing of loops or other verbose code.
I hope this article can help you better understand how these two functions are used and flexibly used in actual development. If you have more questions about PHP, please continue to explore!