array_fill

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

Fills an array with a given value a specified number of times, starting at a chosen index.

C C23

C23
#include <stdio.h>
#include <string.h>

int main(void) {
    // C fills a fixed array with a loop; indices are always 0-based
    const char *xs[3];
    for (int i = 0; i < 3; i++) {
        xs[i] = "a";
    }
    for (int i = 0; i < 3; i++) {
        printf("%s ", xs[i]);
    }
    printf("\n");
    return 0;
}
Output
a a a 

C++ C++26

C++26
#include <iostream>
#include <string>
#include <vector>

int main() {
    // The (count, value) constructor fills directly; indices are always 0-based
    std::vector<std::string> xs(3, "a");
    for (const auto &s : xs) {
        std::cout << s << " ";
    }
    std::cout << "\n";
    return 0;
}
Output
a a a 

C# v11.0.100

v11.0.100
using System.Linq;

// Enumerable.Repeat generates a repeated sequence; indices are always 0-based
var xs = Enumerable.Repeat("a", 3).ToList();
Console.WriteLine(string.Join(", ", xs));
Output
a, a, a

Go v1.27.4

v1.27.4
package main

import "fmt"

func main() {
	// make fixes the length, then a loop fills it; indices are always 0-based
	xs := make([]string, 3)
	for i := range xs {
		xs[i] = "a"
	}
	fmt.Println(xs)
}
Output
[a a a]

Java v27.0.1

v27.0.1
import java.util.Collections;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        // nCopies yields an immutable repeated list; wrap in ArrayList to make it writable
        List<String> xs = new java.util.ArrayList<>(Collections.nCopies(3, "a"));
        System.out.println(xs);
    }
}
Output
[a, a, a]

JavaScript v26.13.1

v26.13.1
// Array(len).fill fills a fixed length; indices are always 0-based
console.log(Array(3).fill("a"));
// Emulating an arbitrary start index: a key-value object
console.log(Object.fromEntries([...Array(3).keys()].map((i) => [i + 2, "a"])));
Output
[ 'a', 'a', 'a' ]
{ '2': 'a', '3': 'a', '4': 'a' }

PHP v8.6.0

v8.6.0
// Fills 3 "a" entries starting at index 2: any integer start index is allowed
print_r(array_fill(2, 3, "a"));
Output
Array
(
    [2] => a
    [3] => a
    [4] => a
)

Python v3.15.2

v3.15.2
# List repetition produces repeated elements; the start index is always 0
xs = ["a"] * 3
print(xs)
# enumerate with a custom start emulates an arbitrary start index
print(dict(enumerate(xs, start=2)))
Output
['a', 'a', 'a']
{2: 'a', 3: 'a', 4: 'a'}

Rust v1.99.1

v1.99.1
fn main() {
    // vec![value; count] is the most idiomatic fill; indices are always 0-based
    let xs = vec!["a"; 3];
    println!("{:?}", xs);
}
Output
["a", "a", "a"]

TypeScript v26.13.1

v26.13.1
// Array(len).fill fills a fixed length; indices are always 0-based
const xs: string[] = Array(3).fill("a");
console.log(xs);
Output
[ 'a', 'a', 'a' ]

Language differences

C: no built-in — hand-roll a loop over a fixed array.

C++: the std::vector constructor (count, value) fills in one step; std::fill also works.

C#: Enumerable.Repeat(value, count) (LINQ).

Go: make plus a loop; slice indices always start at 0.

Java: Collections.nCopies(count, value) returns an immutable list — wrap in new ArrayList<>(...) to make it writable; or Arrays.fill on an existing array.

JavaScript: Array(count).fill(value) (set the length first; new Array(3).map not iterating holes is a classic trap).

PHP: the unique capability is an arbitrary integer $start_index (negatives allowed) from which keys increment; count < 1 throws a ValueError (PHP 8+).

Python: [value] * count is the idiom; list indices start at 0, so arbitrary start keys require a dict.

Rust: vec![value; count]; indices start at 0.

TypeScript: same as JavaScript's Array(count).fill with a type annotation.