PHP array_fill Function: Fill Array with Same Value

PHP array_fill

You can use the array_fill function in PHP when you must fill an array with one value. You may want to set default values in a form or to prepare a list with repeated values for quick use.

Understand the array_fill Function in PHP

The array_fill function creates an array and fills it with one value. You set the start index, the number of elements, and the value.

Here is the syntax:

array_fill($start_index, $count, $value)
  • $start_index – The index for the first element in the array.
  • $count – The number of elements in the array.
  • $value – The value that will fill each element.

PHP makes a loop inside its code when you run the function. The loop starts at the given index and runs until it creates the required number of elements. Each element gets the same value.

You can use this function to prepare arrays with default options. It can also help in tests where you need repeated data. You may also use it when you must fill large arrays quickly.

Here is a quick example:

$array = array_fill(0, 4, 'apple');
print_r($array);

This code creates an array with four items. Each item holds the word “apple”. The index starts from zero and ends at three.

Here is the output:

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

Use the array_fill with Arrays

Create indexed arrays with array_fill:

$list = array_fill(0, 5, 10);
print_r($list);

This will produce indexes from zero to four with the value ten. The output:

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

Use array_fill with numeric indexes:

$data = array_fill(5, 3, 'yes');
print_r($data );

This creates keys five to seven with the value “yes”. The output:

Array
(
[5] => yes
[6] => yes
[7] => yes
)

Combine array_fill with array_map for Dynamic Values:

$base = array_fill(0, 5, 0);
$result = array_map(function($v, $i) { return $i * 2; }, $base, array_keys($base));
print_r($result);

This creates values that depend on the index. The output:

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

So, how does array_fill handle negative indexes?

If you use a negative start index, PHP still creates the array. The index begins at the negative number and grows from there.

For example:

$array = array_fill(-2, 3, 'test');

This creates keys -2, -1, and 0. Here is the output:

Array
(
[-2] => test
[-1] => test
[0] => test
)

Examples of array_fill in PHP

Fill a List with the Same Number:

$numbers = array_fill(0, 5, 100);
print_r($numbers);

This creates five items with the number one hundred. The count begins at index zero. You can use it to prepare lists with fixed values before you add more data.

Start from a Non-zero Index:

$data = array_fill(3, 4, 'ready');
print_r($data);

This fills keys three to six with the word “ready”. It can help when you must keep lower indexes for other data.

Use Negative Index:

$items = array_fill(-3, 3, 'on');
print_r($items);

This starts from index -3 and ends at -1. It is useful when you need to match certain key rules in a program.

Wrapping Up

In this article, you learned the array_fill function and its uses in PHP. You also saw how it works with different types of indexes and how it pairs with other functions.

Here is a quick recap:

  • array_fill creates arrays with repeated values.
  • You can set the start index to zero, a positive number, or a negative number.
  • It works with other functions to produce dynamic results.
  • It works the same in PHP 7 and 8, but error handling differs

FAQs

How to use array_fill in PHP to create a simple array?

$result = array_fill(0, 5, "apple");  
print_r($result);
This code creates an array with 5 elements. Each element has the value apple. Indexes start from 0.

What is the difference between array_fill and array_fill_keys in PHP?

array_fill sets values based on numeric indexes.
$arr1 = array_fill(0, 3, "x");  
print_r($arr1);
array_fill_keys sets values for given keys.
$arr2 = array_fill_keys(["a", "b", "c"], "x");  
print_r($arr2);

Can array_fill in PHP use negative start indexes?

Yes, negative indexes are possible.
$arr = array_fill(-2, 3, "pear");  
print_r($arr);
Indexes will start at -2, then -1, then 0.

How to fill an array with default values using array_fill in PHP?

$defaults = array_fill(0, 4, "N/A");  
print_r($defaults);
This creates 4 elements with the value N/A. Useful for placeholders.

Similar Reads

PHP OOP Constructor: How It Works in a Class with Examples

The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

How to Update MySQL Data with PHP?

Understanding how to update data in PHP and MySQL is like editing a draft—it is all about tweaking the right…

PHP array_map Function: How to Transform Arrays with Examples

PHP added the array_map() function to help you apply one function to each item in an array. Understand the array_map…

PHP array_any: How it Works with Arrays with Examples

PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…

PHP array_intersect_key Function: How it Works with Examples

The array_intersect_key in PHP compares arrays by keys and returns only the parts that match. It is useful when you…

How to Insert Data into MySQL with PHP

pplications. Such As as From registering new users to collecting form submissions and storing product details. Things like adding a…

Parameters and Arguments in PHP: What’s the Difference?

If you're coding in PHP you've most probably come across the terms 'parameters' and 'arguments' in functions. Well, they are…

PHP Boolean: Assigning True or False to a Variable

You can assign the boolean data type to PHP variables or use it as a direct value. This enables you…

filter_has_var Function: Check if Input Exists in PHP

User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

How to Connect PHP to MongoDB

Connecting PHP to MongoDB allows you to integrate database functionalities into your web applications. MongoDB is a NoSQL database that…

Previous Article

Object to Primitive Conversion in JavaScript with Examples

Next Article

HTML samp Tag: How to Show Sample Program Output

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.