Maison > Article > développement back-end > php数组入门教程之数组填充
本文介绍下,有关php数组之数组填充的一个例子,有需要的朋友参考下。
在php编程中,对数组元素进行填充,可以使用array_fill()函数。 来看下面的例子,array_fill()函数——填充数组函数: <?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 ""; ?> 运行结果: 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 ) |