Is there a way to fill up an array with values? I have an array with 5 elements and I want to add the string "filler" to the end until 10 elements are filled.
I tried to find a suitable PHP function, but failed so far. Is there a function at all, or do I have to use a loop?
Thanks
The built-in function array_pad
serves exactly this purpose.
$padded_array = array_pad($source_array,10,"filler");
Make use of array_fill()
<?php
$a = array(0=>1,1=>2,2=>3,3=>4,4=>5);
$b = array_fill(5, 10, 'filler');
$c=array_merge((array)$a, (array)$b);
print_r($c);
OUTPUT:
Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => filler [6] => filler [7] => filler [8] => filler [9] => filler [10] => filler [11] => filler [12] => filler [13] => filler [14] => filler )