The php array_fill() function fills an array with n entries of the same given value with keys starting from given starting index.
$array = array_fill(0, 5, "item");
print_r($array);
// Output:
Array
(
[0] => item
[1] => item
[2] => item
[3] => item
[4] => item
)
When working with collections of data in php, the ability to easily create different collections is valuable.
One such operation which is useful is the ability to create an array with the same value repeated n times.
The php array_fill() function fills an array with n entries of the same value with keys starting from a given starting index.
array_fill() takes three parameters. The first is the starting numeric key. The second parameter is the number of items you want in the array. For the third parameter, you specify the value you want to set for all of the keys.
For example, we can easily create an array of all 0’s of length 5 with array_fill().
$array = array_fill(0, 5, 0);
print_r($array);
// Output:
Array
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
The php array_fill() function can be useful if you need to initialize an array where you know how many elements you will need.
You can pass both positive and negative starting values to the ‘start_index’ parameter of array_fill()
Below is an example of what happens when you pass a negative value to the ‘start_index’ parameter of array_fill().
$array = array_fill(-3, 5, 0);
print_r($array);
// Output:
Array
(
[-3] => 0
[-2] => 0
[-1] => 0
[0] => 0
[1] => 0
)
Creating Multi-Dimensional Arrays with array_fill() Function in php
One useful example of the array_fill() function is if you want to initialize a multi-dimensional array with the same value for all values.
For example, if you are doing simulation or forecasting, sometimes it can be useful to initialize a large multi-dimensional array before simulating so you don’t run into any memory problems.
To create a multi-dimensional array with all the same value in php with array_fill(), you can pass array_fill() to the ‘value’ parameter.
Below is an example of how to create a 5×5 multi-dimensional array of all 0’s in php with array_fill().
$array = array_fill(0, 5, array_fill(0, 5, 0));
echo json_encode($array);
// Output:
[[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0],[0,0,0,0,0]]
Using array_fill_keys() to Specify Keys Before Filling Array in php
If you want to specify the keys of your filled array, instead of using numeric key values determined by a starting point, you can use the array_fill_keys() function.
array_fill_keys() takes two parameters. The first parameter is the array of keys, and the second parameter is the value you want to set for all keys.
Below is an example of how to use array_fill_keys() in php to fill an array with a value given an array of keys.
$keys_array = array("bear","elephant","lion");
print_r(array_fill_keys($keys_array,'animal'));
// Output:
Array
(
[bear] => animal
[elephant] => animal
[lion] => animal
)
Hopefully this article has been useful for you to learn how to use the php array_fill() function to fill an array with the same value.