Home » PHP

PHP array_fill() Function with Example

PHP array_fill() Function: Here, we are going to learn about the array_fill() function with example in PHP.
Submitted by IncludeHelp, on February 10, 2019

PHP array_fill() Function

array_fill() function is used to fill the n elements in an array from given index with the specific value.

Syntax:

    array_fill(index, n, value) : array

Here,

  • index is the starting position from where we have to start filling the elements.
  • n is the total number of elements to be filled from given “index”.
  • value is any string, integer etc value to be filled.

Examples:

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

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

PHP code:

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

Output

Array
(
    [3] => Okay
    [4] => Okay
    [5] => Okay
    [6] => Okay
    [7] => Okay
)
Array
(
    [0] => Hello
    [1] => Hello
    [2] => Hello
    [3] => Hello
    [4] => Hello
)





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.



Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.