PHP Functions to Fill Arrays

PHP provides several functions that fill arrays with values, including array_fill, array_fill_keys, range, and array_pad. These functions all return new arrays and provide various ways to fill their elements. We describe and demonstrate each of these functions below.

array_fill

PHP's array_fill function fills an array with a value you specify. Pass a starting index, the number of elements to fill, and the fill value, and an array meeting those specifications will be returned:

// arguments: starting index, number of elements, fill value
$ar = array_fill(0, 4, 0);
print_r($ar);
/* print_r output (as seen in page source view):
Array
(
    [0] => 0
    [1] => 0
    [2] => 0
    [3] => 0
) */

As you can see in the print_r output, the returned array has a starting index of 0, and a length of 4, with each element containing a 0.

array_fill_keys

The array_fill_keys function is similar to array_fill with the added capability to provide keys for the new array it returns. The first argument is an array that specifies the keys for the returned array. The second argument is the value to assign for each key. The example below creates an associative array of months initializing each value to 0:

// array of keys to pass to array_fill_keys
$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'June', 
        'July', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec');
$ar = array_fill_keys($months, 0);
print_r($ar);
/* Array
(
    [Jan] => 0
    [Feb] => 0
    [Mar] => 0
    [Apr] => 0
    [May] => 0
    [June] => 0
    [July] => 0
    [Aug] => 0
    [Sept] => 0
    [Oct] => 0
    [Nov] => 0
    [Dec] => 0
) */

This would be very handy for tallying monthly totals.

The range Function

The PHP range function fills an array with a series of values. You can specify numbers or characters as the start and end values for your series. The following demonstrates how to fill an array with the values 1 to 10:

// range arguments: start value, end value
$ar = range(1, 10);
print_r($ar);
/* Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [3] => 4
    [4] => 5
    [5] => 6
    [6] => 7
    [7] => 8
    [8] => 9
    [9] => 10
) /*

Here we demonstrate filling an array with the characters a through f:

$ar = range('a', 'f');
print_r($ar);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
) */

When using the range function, the start value can be greater than the end value to create an array with values in descending order:

$ar = range(5, 1);
print_r($ar);
/* Array
(
    [0] => 5
    [1] => 4
    [2] => 3
    [3] => 2
    [4] => 1
) */

The range function includes an optional step argument. We demonstrates how to use it to create an array containing alternate letters from A through M:

// arguments: start value, end value, step
$ar = range('A', 'M', 2);
print_r($ar);
/* Array
(
    [0] => A
    [1] => C
    [2] => E
    [3] => G
    [4] => I
    [5] => K
    [6] => M
) */

The step argument can be floating point if your range is numbers:

$ar = range(.1, 1, .2);
print_r($ar);
/* Array
(
    [0] => 0.1
    [1] => 0.3
    [2] => 0.5
    [3] => 0.7
    [4] => 0.9
) */

Notice that the last element of the returned array is less than the end value passed to the range function, since adding the step value again would result in a value greater than the end value specified.

array_pad

The array_pad function create a new array by copying an existing array that you pass to it and padding it to the length you specify with the value you specify. It takes three arguments: the array you want to copy and pad, the new length you wish it to have, and the value for the new elements. The following demonstrates:

$ar = array('a', 'b', 'c');
// pad $ar to length of 8 with character 'd'
$ar2 = array_pad($ar, 8, 'd');
print_r($ar2);
/* Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => d
    [5] => d
    [6] => d
    [7] => d
) */

Specify a positive number for the size argument to pad the array on the right, as shown above. A negative number inserts new elements at the start of the returned array:

$ar = array('a', 'b', 'c');
// pass negative size to pad beginning of $ar
$ar2 = array_pad($ar, -6, 'z');
print_r($ar2);
/* Array
(
    [0] => z
    [1] => z
    [2] => z
    [3] => a
    [4] => b
    [5] => c
) */

Back to top