Home  >  Article  >  Backend Development  >  PHP function array_fill() that fills an array with a given key value

PHP function array_fill() that fills an array with a given key value

黄舟
黄舟Original
2017-11-07 10:40:461277browse

Example

Fill the array with the given key value:

<?php
$a1=array_fill(3,4,"blue");
print_r($a1);
?>

Definition and usage

array_fill() function fills the array with the given key value.

Syntax

array_fill(index,number,value);
Parameters Description
index Required. Specifies the starting index of the returned array.
number Required. Specifies the number of elements to fill. Its value must be greater than 0.
value Required. Specifies the key values ​​used to populate the array.

Technical Details

Return value: Returns the populated array.
PHP Version: 4.2+

Example

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

Output:

Array ( [2] => Dog [3] => Dog [4] => Dog )

can be filled with the following statements:

$abc=0;
for($i=0;$i<3;$i++) {
 for($j=0;$j<2;$j++) {
 $abc=$abc+1;
 $creation[$i][$j]=$abc;
 }
}

The result is:

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

The above is the detailed content of PHP function array_fill() that fills an array with a given key value. 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