PHP array_fill() function


The array_fill() function in PHP is used to create an array and fill it with a specified value. It allows you to generate an array where all the elements have the same value. This can be useful in various scenarios, such as initializing an array with default values.

Syntax:

array_fill(int $start_index, int $count, mixed $value): array

Parameters:

  • $start_index: The index at which to start filling the array. This can be positive (starting from the beginning) or negative (starting from the end).
  • $count: The number of elements to fill in the array. If this value is less than or equal to 0, the function returns an empty array.
  • $value: The value that will be used to fill the array. This can be any data type (e.g., integer, string, object, etc.).

Return Value:

  • The function returns an array filled with the specified value for the given number of elements, starting at the specified index.

Example 1: Basic Usage

<?php // Fill an array with 5 elements, all having the value "apple", starting at index 0 $array = array_fill(0, 5, "apple"); print_r($array); ?>

Output:

Array ( [0] => apple [1] => apple [2] => apple [3] => apple [4] => apple )

In this example, the array is filled with 5 occurrences of the value "apple", starting at index 0.

Example 2: Negative Start Index

You can use a negative $start_index to fill an array starting from the end.

<?php // Fill an array starting from index -3, with 4 elements, all having the value "banana" $array = array_fill(-3, 4, "banana"); print_r($array); ?>

Output:

Array ( [-3] => banana [-2] => banana [-1] => banana [0] => banana )

In this case, the array starts filling from index -3 and fills 4 positions, with each position containing the value "banana".

Example 3: Zero or Negative Count

If the $count parameter is 0 or negative, array_fill() will return an empty array.

<?php // Attempt to fill an array with 0 elements $array = array_fill(0, 0, "value"); print_r($array); ?>

Output:

Array ( )

Example 4: Different Data Types

The value used to fill the array can be of any data type, including numbers, strings, or even objects.

<?php // Fill an array with 3 elements, all having the number 100 $array = array_fill(0, 3, 100); print_r($array); ?>

Output:

Array ( [0] => 100 [1] => 100 [2] => 100 )

In this example, the array is filled with three occurrences of the integer 100.

Practical Usage:

  • array_fill() is useful for:
    • Initializing arrays with default values (e.g., creating placeholders).
    • Setting up arrays to later be filled with data dynamically.
    • Creating predefined structures for testing or prototyping.

Limitations:

  • If you need to fill an array with key-value pairs, use other functions like array_fill_keys() instead of array_fill(), which only allows for indexed filling.

Summary:

  • array_fill($start_index, $count, $value) creates and returns an array where all elements are filled with a specified value.
  • You can define the number of elements and the starting index.
  • It’s a simple yet powerful function for generating arrays with preset values.