Jesus · Bible · HTML · CSS · JS · PHP · SVG · Applications

array_fill

Description

The array_fill of Array for PHP fill an array with values.

Syntax

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

Parameters

start_index

The first index of the returned array.

count

Number of elements to insert. Must be greater than or equal to zero, and less than or equal to 2147483647.

value

Value to use for filling

Return

Returns the filled array

Examples

1 · start_index · Negative

<?
    
$start_index = -5;
$count = 5;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
    [-5] => abc
    [-4] => abc
    [-3] => abc
    [-2] => abc
    [-1] => abc
)

2 · start_index · Zero

<?
    
$start_index = 0;
$count = 5;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

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

3 · start_index · Positive

<?
    
$start_index = 5;
$count = 5;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
    [5] => abc
    [6] => abc
    [7] => abc
    [8] => abc
    [9] => abc
)

4 · count · Zero

<?
    
$start_index = 0;
$count = 0;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
)

5 · count · Positive

<?
    
$start_index = 0;
$count = 10;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
    [0] => abc
    [1] => abc
    [2] => abc
    [3] => abc
    [4] => abc
    [5] => abc
    [6] => abc
    [7] => abc
    [8] => abc
    [9] => abc
)

6 · value · Boolean

<?
    
$start_index = 0;
$count = 5;
$value = true;

$return = array_fill($start_index, $count, $value);

print_r($return);

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

7 · value · Integer

<?
    
$start_index = 0;
$count = 5;
$value = 0;

$return = array_fill($start_index, $count, $value);

print_r($return);

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

8 · value · Float

<?
    
$start_index = 0;
$count = 5;
$value = 1.2;

$return = array_fill($start_index, $count, $value);

print_r($return);

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

9 · value · String

<?
    
$start_index = 0;
$count = 5;
$value = "abc";

$return = array_fill($start_index, $count, $value);

print_r($return);

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

10 · value · Array

<?
    
$start_index = 0;
$count = 5;
$value = array(0, 1);

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
    [0] => Array
        (
            [0] => 0
            [1] => 1
        )

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

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

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

    [4] => Array
        (
            [0] => 0
            [1] => 1
        )

)

11 · value · Object

<?
    
$start_index = 0;
$count = 5;
$value = new stdclass;

$return = array_fill($start_index, $count, $value);

print_r($return);

?>
Array
(
    [0] => stdClass Object
        (
        )

    [1] => stdClass Object
        (
        )

    [2] => stdClass Object
        (
        )

    [3] => stdClass Object
        (
        )

    [4] => stdClass Object
        (
        )

)
HomeMenu