Home > Article > Backend Development > PHP array introductory tutorial - array filling
This article introduces an example of array filling in PHP arrays. Friends in need can refer to it.
In PHP programming, to fill array elements, you can use the array_fill() function. Look at the following example, array_fill() function - fill array function: <?PHP //数组填充 //array_fill()例子 //by bbs.it-home.org $array = range(1,10); $fillarray = range("a","d"); $arrayFilled = array_fill(0,5,$fillarray);//$fillarray可以是字符串,如"test". echo "<pre class="brush:php;toolbar:false">"; print_r ($arrayFilled); echo ""; $keys = array("string","2",9,"SDK","PK"); $array2 = array_fill_keys($keys,"testing"); echo " "; print_r ($array2); echo ""; ?> Run result: Array ( [0] => Array ( [0] => a [1] => b [2] => c [3] => d ) [1] => Array ( [0] => a [1] => b [2] => c [3] => d ) [2] => Array ( [0] => a [1] => b [2] => c [3] => d ) [3] => Array ( [0] => a [1] => b [2] => c [3] => d ) [4] => Array ( [0] => a [1] => b [2] => c [3] => d ) ) Array ( [string] => testing [2] => testing [9] => testing [SDK] => testing [PK] => testing ) |