W3docs

The Ultimate Guide to PHP's array_fill Function

The array_fill function in PHP is a powerful tool for creating arrays filled with values of your choice. In this guide, we'll take a deep dive into how to use

The array_fill function in PHP is a powerful tool for creating arrays filled with values of your choice. In this guide, we'll take a deep dive into how to use this function and how it can be applied in real-world scenarios.

What is the array_fill Function in PHP?

The array_fill function is a built-in PHP function that takes three arguments: the start index, the number of elements, and the value to fill the array with. The function returns an array with the specified number of elements, each with the specified value.

How to Use the array_fill Function in PHP

Using the array_fill function is straightforward. Simply specify the start index, number of elements, and the value you want to fill the array with, and the function will return an array with the specified values.

Here's an example of how to use the array_fill function to create an array of 10 elements, starting at index 0, filled with the value "PHP":

PHP Example of array_fill function usage

<?php

$array = array_fill(0, 10, "PHP");
print_r($array);

?>

The resulting array will look like this:


Array (
  [0] => PHP
  [1] => PHP
  [2] => PHP
  [3] => PHP
  [4] => PHP
  [5] => PHP
  [6] => PHP
  [7] => PHP
  [8] => PHP
  [9] => PHP
)

Real-World Applications of the array_fill Function in PHP

The array_fill function can be used in a variety of real-world scenarios. Here are some examples:

Initializing Arrays

One of the most common uses for the array_fill function is to initialize arrays with default values. For example, you could use the function to create an array of default values for a form field.

Creating Multidimensional Arrays

The array_fill function can also be used to create multidimensional arrays. For example, you could use the function to create a 2D array filled with zeros:

PHP multi dimension usage of array_fill function

<?php

$array = array_fill(0, 10, array_fill(0, 10, 0));
print_r($array);

?>

This will create a 10x10 array filled with zeros.

Note: array_fill stores references to the same inner array for each row. Modifying one row will modify all rows. To create independent rows, use a loop to generate separate inner arrays.

Filling Arrays with a Single Value

The array_fill function repeats a single value across multiple indices. For example, you could use it to create an array of 10 elements starting at index 1, all filled with the value 5:

PHP Example fill an array with a repeated value

<?php

$array = array_fill(1, 10, 5);
print_r($array);

?>

This will create an array with 10 elements, starting at index 1, filled with the value 5.

Note: To generate an array with incremental values (e.g., numbers 1 to 10), use the built-in range(1, 10) function instead.

Conclusion

The array_fill function in PHP is a powerful tool for creating arrays filled with values of your choice. Whether you're initializing arrays, creating multidimensional arrays, or filling arrays with incremental values, this function is a great solution. We hope this guide has helped you learn how to use the array_fill function in PHP.

Practice

Practice

What does the array_fill() function in PHP do?