PHP array_fill Function
last modified March 13, 2025
The PHP array_fill
function creates an array filled with a
specified value. It's useful for initializing arrays with default values.
Basic Definition
The array_fill
function generates an array with values. It
starts at a specified index and fills the array with the given value.
Syntax: array_fill(int $start_index, int $count, mixed $value): array
.
The function returns a new array with $count
elements.
Basic array_fill Example
This demonstrates creating a simple array filled with a default value.
<?php $filledArray = array_fill(0, 5, 'default'); print_r($filledArray);
This creates an array with 5 elements, all set to 'default'. The array starts at index 0. The output shows all elements have the same value.
Negative Start Index
array_fill
can create arrays with negative starting indices.
<?php $negativeArray = array_fill(-3, 4, 'item'); print_r($negativeArray);
This creates an array starting at index -3 with 4 elements. The resulting array will have keys -3, -2, -1, and 0, all filled with 'item'.
Filling with Different Data Types
The function can fill arrays with any data type, including objects.
<?php $numberArray = array_fill(0, 3, 42); $boolArray = array_fill(0, 2, true); $objectArray = array_fill(0, 2, new stdClass()); print_r($numberArray); print_r($boolArray); print_r($objectArray);
This shows arrays filled with integers, booleans, and objects. Note that object elements will be references to the same object instance.
Multi-dimensional Arrays
array_fill
can be used to create multi-dimensional arrays.
<?php $matrix = array_fill(0, 3, array_fill(0, 3, 0)); print_r($matrix);
This creates a 3x3 matrix filled with zeros. The outer array_fill
creates an array where each element is another array filled with zeros.
Combining with array_fill_keys
array_fill
can be combined with other array functions.
<?php $keys = ['a', 'b', 'c']; $filled = array_fill_keys($keys, array_fill(0, 3, null)); print_r($filled);
This creates an associative array where each key maps to a sub-array filled
with null values. It demonstrates combining array_fill
with
array_fill_keys
.
Best Practices
- Memory Usage: Be cautious with large arrays as they consume memory.
- Object References: Remember all object elements reference the same instance.
- Negative Indices: Useful for specific use cases but can be confusing.
- Performance: Faster than manual loops for array initialization.
Source
This tutorial covered the PHP array_fill
function with practical
examples showing its usage for array initialization scenarios.
Author
List all PHP Array Functions.