PHP array_fill() Function with Example

PHP array_fill() Function: Here, we will find out about the array_fill() work with example in PHP.

PHP array_fill() Function

array_fill() work is utilized to fill the n components in a cluster from given file with the particular value.

Syntax:

array_fill(index, n, value) : array

Here,

  • record is the beginning situation from where we need to begin filling the components.
  • n is the all outnumber of components to be filled from given “file“.
  • value is any string, integer and so forth value to be filled.

Example:

    Input:
    index = 3
    n = 5
    value = "Kishan"

    Output:
    Array
    (
        [3] => Kishan
        [4] => Kishan
        [5] => Kishan
        [6] => Kishan
        [7] => Kishan
    )

PHP Code:

<?php
	//filling from 3rd index 
	$arr1 = array_fill(3, 5, "Kishan");	
	print_r ($arr1); 
	
	//filling from 0th index to next 5
	//i.e. from index 0 to 4
	$arr2 = array_fill(0, 5, "Durgesh");
	print_r ($arr2); 
?>

Output:

Array
(
    [3] => Kishan
    [4] => Kishan
    [5] => Kishan
    [6] => Kishan
    [7] => Kishan
)
Array
(
    [0] => Durgesh
    [1] => Durgesh
    [2] => Durgesh
    [3] => Durgesh
    [4] => Durgesh
)

Leave a Comment

error: Alert: Content is protected!!