Creating a Zero-Filled Array in PHP
Last Updated :
16 Jul, 2024
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
)
Similar Reads
How to convert an array to CSV file in PHP ? To convert an array into a CSV file we can use fputcsv() function. The fputcsv() function is used to format a line as CSV (comma separated values) file and writes it to an open file. The file which has to be read and the fields are sent as parameters to the fputcsv() function and it returns the leng
2 min read
What is the most efficient way to create a zero filled array in JavaScript ? Creating zero-filled arrays is a common task in JavaScript, essential for initializing data structures, matrices, and more. This guide explores the most efficient methods to create zero-filled arrays, enhancing performance and code readability. Syntax: arr(length).fill(value);Importance of Zero-Fill
2 min read
Remove First Element from an Array in PHP Given an array, the task is to remove the first element from an array in PHP. Examples:Input: arr = [1, 2, 3, 4, 5, 6, 7]; Output: 2, 3, 4, 5, 6, 7 Input: arr = [3, 4, 5, 6, 7, 1, 2] Output: 4, 5, 6, 7, 1, 2Below are the methods to remove the first element from an array in PHP:Table of ContentUsing
3 min read
Associative Arrays in PHP An associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These
4 min read
PHP array_fill() function The array_fill() is an inbuilt-function in PHP and is used to fill an array with values. This function basically creates an user-defined array with a given pre-filled value. Syntax: array_fill($start_index, $number_elements, $values) Parameter: The array_fill() function takes three parameters and ar
2 min read
How to create an array with key value pairs in PHP? In PHP, an array with key-value pairs is called an associative array. It maps specific keys to values, allowing you to access elements using custom keys rather than numerical indices. Keys are strings or integers, while values can be of any data type.Here we have some common approaches to create an
2 min read
Create an array filled with given values Using JavaScript In JavaScript, an array is a collection of values that can be stored and accessed in a single variable. Each value in an array is called an element, and elements can be of any data type, such as numbers, strings, or other objects. Arrays are indexed, meaning that each element has a numerical positio
5 min read
How to Insert a New Element in an Array in PHP ? In PHP, an array is a type of data structure that allows us to store similar types of data under a single variable. The array is helpful to create a list of elements of similar types, which can be accessed using their index or key.We can insert an element or item in an array using the below function
5 min read
How to Slice an Array in PHP? In PHP, slicing an array means taking a subset of the array and extracting it according to designated indices. When you need to extract a subset of elements from an array without changing the original array, this operation comes in handy. PHP comes with a built-in function called array_slice() to he
2 min read
PHP array_filter() Function This built-in function in PHP is used to filter the elements of an array using a user-defined function which is also called a callback function. The array_filter() function iterates over each value in the array, passing them to the user-defined function or the callback function. If the callback func
2 min read