Worldscope

PHP array_fill() Function

Palavras-chave:

Publicado em: 05/08/2025

PHP array_fill() Function: Populating Arrays with Ease

The array_fill() function in PHP is a powerful tool for creating arrays where a specific value is repeated a certain number of times. This article provides a comprehensive guide to understanding and using the array_fill() function, including its parameters, functionality, and applications.

Fundamental Concepts / Prerequisites

To effectively understand the array_fill() function, a basic understanding of PHP arrays is required. Familiarity with array indexing and the concept of associative and indexed arrays is helpful. Prior knowledge of basic PHP syntax is also assumed.

Core Implementation/Solution

The array_fill() function takes three parameters: a starting index, the number of elements to fill, and the value to fill them with. It returns a new array with the specified range of indices populated with the given value.


<?php

/**
 * Demonstrates the usage of the array_fill() function.
 *
 * @param int $startIndex The starting index of the array.
 * @param int $num The number of elements to fill.
 * @param mixed $value The value to fill the array with.
 * @return array The resulting array.
 */
function demonstrateArrayFill(int $startIndex, int $num, $value): array
{
  $filledArray = array_fill($startIndex, $num, $value);
  return $filledArray;
}

// Example usage:
$start = 2;
$number = 5;
$fillValue = "example";

$resultArray = demonstrateArrayFill($start, $number, $fillValue);

print_r($resultArray);

//Expected output: Array ( [2] => example [3] => example [4] => example [5] => example [6] => example )
?>

Code Explanation

The code snippet defines a function demonstrateArrayFill() that takes three arguments: $startIndex, $num, and $value. The array_fill() function is then called with these arguments. $startIndex specifies the index from which the array population should begin. $num indicates the number of elements to fill in the array, starting from $startIndex. Finally, $value is the value that will be used to populate each of these elements. The resulting array, $filledArray, is then returned. The example usage section shows how to call this function and prints the resulting array using print_r() to display the array's structure and content.

Complexity Analysis

The array_fill() function has a time complexity of O(n), where n is the number of elements to fill. This is because it iterates through the specified number of elements to assign the given value. The space complexity is also O(n), as it creates a new array with n elements.

Alternative Approaches

While array_fill() is the most direct way to achieve this, you could also use a loop to manually create the array. For example:


<?php
$array = [];
$start = 2;
$number = 5;
$fillValue = "example";

for ($i = $start; $i < $start + $number; $i++) {
    $array[$i] = $fillValue;
}

print_r($array);

// Expected output: Array ( [2] => example [3] => example [4] => example [5] => example [6] => example )
?>

This approach is less concise and potentially less efficient than using array_fill() directly, especially for larger arrays, due to the overhead of managing the loop.

Conclusion

The array_fill() function provides a simple and efficient way to create arrays with a specified number of elements initialized to the same value. Understanding its parameters and functionality allows developers to quickly and easily populate arrays for various purposes, making it a valuable tool in PHP development. While alternative approaches exist, array_fill() offers a more streamlined and generally faster solution.