Open In App

How to Create an Array of Given Size in PHP?

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

This article will show you how to create an array of given size in PHP. PHP arrays are versatile data structures that can hold multiple values. Sometimes, it’s necessary to create an array of a specific size, either filled with default values or left empty.

Create an Array of Given Size using array_fill() Function

The array_fill() function is a basic method to create an array of a specified size, filled with a given value.

PHP
<?php

function createArray($size, $value) {
    return array_fill(0, $size, $value);
}

// Driver code
$size = 5;
$value = 'Geeks';
$array = createArray($size, $value);

print_r($array);

?>

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

Explanation:

  • createArray Function: This function takes the desired size and the value to fill the array with as arguments.
  • array_fill Function: Creates an array filled with the specified value. The first argument is the starting index (usually 0), the second is the size of the array, and the third is the value to fill.

Create an Array of Given Size using range() and array_map() Functions

For creating an array of a given size with default values, you can use a combination of range() and array_map() functions.

PHP
<?php

function createArray($size, $value) {
    return array_map(function() use (
        $value) { return $value; }, 
        range(1, $size)
    );
}

// Driver code
$size = 5;
$value = 'Geeks';
$array = createArray($size, $value);

print_r($array);

?>

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

Explanation:

  • createArray Function: This function uses range() to create an array of the specified size and array_map() to fill it with the desired value.
  • range() Function: Generates an array containing a range of elements. In this case, it’s used to create an array with a length equal to $size.
  • array_map() Function: Applies the callback function to each element of the array generated by range(), effectively filling the array with $value.

Create an Array of Given Size using a Loop

You can also use a loop to create an array of a given size, manually adding elements until the array reaches the desired length.

PHP
<?php

function createArray($size, $value) {
    $array = [];
    for ($i = 0; $i < $size; $i++) {
        $array[] = $value;
    }
    return $array;
}

// Driver code
$size = 5;
$value = 'Geeks';
$array = createArray($size, $value);

print_r($array);

?>

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

Explanation:

  • createArray Function: This function initializes an empty array and uses a for loop to add the specified value to the array until it reaches the desired size.
  • $array[] = $value; Statement: Appends the value to the array in each iteration of the loop.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads