This article demonstrates some PHP functions which are used to fill an array with values. The list and details of these functions are given below :
1. array_fill()
array_fill() function is used to fill an array with a specified value.
Syntax : array_fill($start, $number, $value)
$start : Specifies the starting index for returning array.(Required)
$number : Specifies the number of elements to be inserted into the array.(Required)
$value : Specify the value to be filled in the array.(Required)
Description : This function fills $number elements in an array with a specified value defined by $value parameter. The $start parameter defines the starting index of the returned array. If negative(-ve) value is passed for $start parameter, then only first index will be $start and other following indexes will be starting from 0.
Note : $number parameter always has a value equal to or greater than 0. This parameter cannot accept (-) negative value.
Example :
<?php
$arr = array();
$arr = array_fill(2, 4, 'Test');
print_r($arr);
echo "<br>";
$arr = array_fill(-2, 4, 'Test');
print_r($arr);
?>
Output :
Array ( [2] => Test [3] => Test [4] => Test [5] => Test )
Array ( [-2] => Test [0] => Test [1] => Test [2] => Test )
2. array_fill_keys()
array_fill_keys() function filled an array with specified value on specified keys.
Syntax : array_fill_keys($key_arr, $value)
$key_arr : Specifies an array of keys(indexes).(Required)
$value : Specify the value used for filling array.(Required)
Description : array_fill_keys() accepts two parameters, first parameter specify an array of values which are used as keys for returned array. Second parameter is value which is inserted as value on each specified key by first parameter.
Example :
<?php
$arr = $key_arr = array();
$key_arr = array('one', 2, 3, 'four');
$arr = array_fill_keys($key_arr, 'Test');
print_r($arr);
?>
Output :
Array ( [one] => Test [2] => Test [3] => Test [four] => Test )
3. array_pad()
The array_pad() function is used to add(insert) elements with a specified value into an array to increase the array length upto specified length.
Syntax : array_pad($array, $length, $value)
$array : Specifies an array.(Required)
$length : Specifies the length of returning array after padding.(Required)
$value : Specify the value used for padding.(Required)
Description : array_pad() add new elements to array to pad the array to the size specified by $length with $value. The function returns an array with elements from a first array and newly added element with specified value.
The padding(insertion) of elements takes under these conditions :
1. If $length is positive integer, new elements added on the right side(at end) of array. With negative value new elements added on left side(from beginning) of array.
2. If the absolute value of $length field is less than or equal to size of $array then no new element added to the resultant array.
Example :
<?php
$arr = array();
$fruits = array("apple","orange");
$arr = array_pad($fruits, 6,'mango');
print_r($arr);
echo "<br><br>";
echo "With negative length : <br>";
$arr = array_pad($fruits, -6,'mango');
print_r($arr);
?>
Output :
Array ( [0] => apple [1] => orange [2] => mango [3] => mango [4] => mango [5] => mango )
With negative length :
Array ( [0] => mango [1] => mango [2] => mango [3] => mango [4] => apple [5] => orange )
0 Comment(s)