Home  >  Article  >  Backend Development  >  How to use array_fill function in php

How to use array_fill function in php

藏色散人
藏色散人Original
2019-05-24 14:05:263455browse

Usage of array_fill function in php: [array_fill(index,number,value)]. The array_fill function is used to fill the given key value into an array and return the filled array.

How to use array_fill function in php

php The array_fill function is to fill the array with key values. Its syntax is array_fill(index,number,value). The parameter index is required and is the returned array. The first index; number is required and specifies the number of elements to be inserted; value is required and specifies the value used to fill the array.

(Recommended tutorial: php video tutorial)

How to use the php array_fill function?

Function: Fill the array with key values.

Syntax:

array_fill(index,number,value)

Parameters:

index Required. The first index of the returned array.

number Required. Specifies the number of elements to insert.

value Required. Specifies the values ​​used to populate the array.

Description:

Fill the array with the given value. The returned array has number elements and the value is value. The returned array is numerically indexed, starting at the start position and increasing. If number is 0 or less than 0, an error occurs.

php array_fill() function usage example 1

<?php
$a=array_fill(2,3,"无忌");
print_r($a);
?>

Output:

Array ( [2] => 无忌 [3] => 无忌 [4] => 无忌 )

php array_fill() function usage example 2

<?php
$a=array_fill(1,2,"西门");
print_r($a);
?>

Output:

Array ( [1] => 西门 [2] => 西门 )

The above is the detailed content of How to use array_fill function in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn