php数组入门教程之数组填充

发布时间:2019-12-17编辑:脚本学堂
本文介绍下,有关php数组之数组填充的一个例子,有需要的朋友参考下。

php编程中,对数组元素进行填充,可以使用array_fill()函数。

来看下面的例子,array_fill()函数——填充数组函数:

<?PHP
//数组填充 
//array_fill()例子
//by www.jb200.com
$array = range(1,10);
$fillarray = range("a","d");
$arrayFilled = array_fill(0,5,$fillarray);//$fillarray可以是字符串,如"test".
echo "<pre>";
print_r ($arrayFilled);
echo "</pre>";
$keys = array("string","2",9,"SDK","PK");
$array2 = array_fill_keys($keys,"testing");
echo "<pre>";
print_r ($array2);
echo "</pre>";
?>

运行结果:

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
)