PHP Programing language

array_fill() Functions in PHP
Previous Home Next
adplus-dvertising

array_fill()

The array_fill() function fills an array with values.

Syntax:
array_fill(index,number,value);

In above syntax, "index" is the first index of the returned array, "number" specifies the number of elements to insert and "value" specifies the value to use for filling the array.

Example:

<?php
$array1=array_fill(2,3,"Mango");
$array2=array_fill(0,1,"Orange");
print_r($array1);
echo "<br>";
print_r($array2);
?>

Output:

Array ( [2] => Mango [3] => Mango [4] => Mango ) 
Array ( [0] => Orange )

array_fill_keys()

The array_fill_keys() function fills an array with values, specifying keys.

Syntax:
array_fill_keys(keys,value);

In above syntax, "keys" are array of values that will be used as keys and "value" specifies the value to use for filling the array

Example:

<?php
$keys=array("1","2","3","4");
$array1=array_fill_keys($keys,"Mango");
print_r($array1);
?>

Output:

Array ( [1] => Mango [2] => Mango [3] => Mango [4] => Mango )

Previous Home Next