Creating a Zero-Filled Array in PHP
Last Updated :
23 Jul, 2025
Creating an array filled with zeros can be useful in various scenarios, such as initializing an array for further manipulation or setting up a default state. This article explores multiple approaches to creating a zero-filled array in PHP. A zero-filled array is an array where each element is initialized to zero. This can be achieved using different PHP functions and methods.
These are the following methods:
Using array_fill() Function
The array_fill() function is a straightforward way to create an array filled with a specified value.
Example: array_fill(0, $length, 0) creates an array starting at index 0, with $length elements, each initialized to 0.
PHP
<?php
function zeroFilledArr($len) {
return array_fill(0, $len, 0);
}
// Driver code
$len = 5;
$arr = zeroFilledArr($len);
print_r($arr);
?>
OutputArray
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
Using a for Loop
A for loop can be used to manually populate an array with zeros.
Example: An empty array is initialized. The for loop iterates from 0 to $length - 1, appending 0 to the array on each iteration.
PHP
<?php
function zeroFilledArr($len) {
$arr = [];
for ($i = 0; $i < $len; $i++) {
$arr[] = 0;
}
return $arr;
}
// Driver code
$len = 5;
$arr = zeroFilledArr($len);
print_r($arr);
?>
OutputArray
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
Using array_map() and range() Functions
The combination of array_map() and range() can create a zero-filled array.
- range(1, $length) generates an array of integers from 1 to $length.
- array_map() applies a callback function that returns 0 for each element in the range array, resulting in a zero-filled array.
Example: This example shows the use of array_map() and range() function to create zero filled array.
PHP
<?php
function zeroFilledArr($len) {
return array_map(function() {
return 0;
}, range(1, $len));
}
// Driver code
$len = 5;
$arr = zeroFilledArr($len);
print_r($arr);
?>
OutputArray
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
Using array_pad() Function
The array_pad() function can pad an array to a specified length with a specified value, starting from an initial empty array.
Example: array_pad([], $length, 0) creates an empty array and pads it to $length elements, each initialized to 0.
PHP
<?php
function zeroFilledArr($len) {
return array_pad([], $len, 0);
}
// Driver code
$len = 5;
$arr = zeroFilledArr($len);
print_r($arr);
?>
OutputArray
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
)
Using array_fill_keys Function
Another approach to create a zero-filled array in PHP is by using the array_fill_keys function. This function is particularly useful when you have a set of specific keys and want to initialize each of them with a zero value.
Example: The following example show how to use the array_fill_keys function to create a zero-filled array.
PHP
<?php
$keys = range(0, 9); // Creating a range of keys from 0 to 9
$zeroFilledArray = array_fill_keys($keys, 0);
print_r($zeroFilledArray);
?>
OutputArray
(
[0] => 0
[1] => 0
[2] => 0
[3] => 0
[4] => 0
[5] => 0
[6] => 0
[7] => 0
[8] => 0
[9] => 0
)