array_fill(): Filling an array with values

Current PHP usage

array_fill() creates an indexed array containing a requested number of copies of the same value. You control the starting integer index, number of elements and fill value.

$status = array_fill(0, 4, 'pending');
print_r($status);

The number of elements must be valid for the current PHP runtime; use the returned array like any other indexed array.

Fills an array with given value.
$new_array = array_fill ($start_key, $number, $value);
ParameterDESCRIPTION
$start_keyRequired : Integer , starting key number.
$numberRequired: Integer: Number of elements filled.
$valueRequired: value to be used to fill the array.
Returns an array with $number of $values as elements starting from key $start_key.

Example 1

$result=array_fill(2,4,'plus2net');
foreach ($result as $key => $val) {
echo "$key -> $val <br>";
}
Output is here.
2 -> plus2net 
3 -> plus2net 
4 -> plus2net 
5 -> plus2net

Example 2 with negative $start_key

$result=array_fill(-3,4,'plus2net');
foreach ($result as $key=>$val) { // PHP 8
echo "$key -> $val <br>";
}
Output
-3 -> plus2net 
-2 -> plus2net 
-1 -> plus2net 
0 -> plus2net 

Filling array with Keys and values

Example 3: Filling an Array with Default Values for Form Processing

When processing forms with multiple inputs, array_fill() can help initialize empty fields with default values. This is useful to avoid undefined index errors.

$input_fields = array_fill(0, 5, '');
print_r($input_fields);

This will create an array of 5 elements, each initialized to an empty string, which can be assigned to form inputs.

Example 4: Filling an Associative Array

While array_fill() creates indexed arrays, we can simulate an associative array by using custom keys:

$keys = ['name', 'email', 'age'];
$filled_array = array_combine($keys, array_fill(0, count($keys), 'default'));
print_r($filled_array);

This example fills an array with predefined keys, assigning 'default' to each key.

Example 5: Filling an Array Conditionally Based on Values

In some cases, you may want to fill an array only under certain conditions. This example demonstrates filling an array if a value meets a condition:

$numbers = range(1, 10);
$filled_array = array_map(function($num) {
    return $num % 2 == 0 ? 'even' : $num;
}, $numbers);
print_r($filled_array);

This creates an array of numbers but replaces even numbers with the string 'even'.

Example 6: Filling a Multi-dimensional Array

You can also use array_fill() to create multi-dimensional arrays by nesting the function:

$matrix = array_fill(0, 3, array_fill(0, 3, 0));
print_r($matrix);

This creates a 3x3 matrix (two-dimensional array) filled with zeros.

Use Case: Preallocating Arrays for Memory Efficiency

In performance-critical applications, preallocating arrays can help optimize memory usage, especially when you know the array's size in advance:

$preallocated_array = array_fill(0, 1000000, null);

This creates a large array of 1,000,000 elements, each initialized to *null*, which can be filled later.

Example 7: Combining array_fill() with array_merge() for Flexible Initialization

We can combine array_fill() with other array functions for more flexibility:

$default_values = array_fill(0, 5, 'N/A');
$additional_values = ['age' => 25, 'name' => 'John'];
$final_array = array_merge($additional_values, $default_values);
print_r($final_array);

This merges predefined values with default ones, ensuring that the array has all required keys.


Array REFERENCE


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer