larray可以是字符串,如"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
=> b
=> c
=> d
)
=> Array
(
[0] => a
=> b
=> c
=> d
)
=> Array
(
[0] => a
=> b
=> c
=> d
)
=> Array
(
[0] => a
=> b
=> c
=> d
)
=> Array
(
[0] => a
=> b
=> c
=> d
)
)
Array
(
[string] => testing
=> testing
=> testing
[SDK] => testing
[PK] => testing
)
三、数组的遍历:
1.foreach遍历: foreach (array_expression as $value){}
foreach (array_expression as $key => $value){}
闲话少说,上实例:
复制代码 代码如下:
<?PHP
$speed = array(50,120,180,240,380);
foreach($speed as $keys=>$values){
echo $keys."=>".$values."<br />";
}
?>
运行结果:
复制代码 代码如下:
0=>50
1=>120
2=>180
3=>240
4=>380
2.while循环遍历:
while循环遍历一般结合list函数,以下是实例
复制代码 代码如下:
<?PHP
$staff = array(
array("姓名","性别","年龄"),
array("小张","男",24),
array("小王","女",25),
array("小李","男",23)
);
echo "<table border=2>";
while(list($keys,$value) = each($staff)){
list($name,$sex,$age) = $value;
echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";
}
echo "</table>";
?>
运行结果:
3.for循环遍历:
复制代码 代码如下:
<?PHP
$speed = range(0,220,20);
for($i =0;$i<count($speed);$i++) {
echo $speed[$i]." ";
}
?>
运行结果:
复制代码 代码如下:
0 20 40 60 80 100 120 140 160 180 200 220
四、数组的指针操作:
涉及函数包括reset、prev、end、next、current、each
实例一:
复制代码 代码如下:
<?PHP
$speed = range(0,220,20);
echo current($speed);//输出当前位置的值(在数组的开头位置)
$i = rand(1,11);
while($i--){
next($speed);//指针从当前位置向后移动一位
}
echo current($speed);//输出当前位置的值
echo "<br />";
echo prev($speed);//输出前一位置数组值
echo "<br />";
echo reset($speed);//重置数组的指针,将指针指向起始位置
echo "<br />";
echo end($speed);//输出最后位置的数组值
echo "<br />";
?>
运行结果:
复制代码 代码如下:
0220
200
0
220
实例二:each函数指针操作
复制代码 代码如下:
<?PHP
$speed = range(0,200,40);
echo "each实现指针下移 <br />";
echo "0挡的速度是".current(each($speed))."<br />";
echo "1挡的速度是".current(each($speed))."<br />";
echo "2挡的速度是".current(each($speed))."<br />";
echo "3挡的速度是".current(each($speed))."<br />";
echo "4挡的速度是".current(each($speed))."<br />";
echo "5挡的速度是".current(each($speed))."<br />";
echo "使用each函数实现数组指针的移动,进行数组遍历 <br />";
reset($speed);//这里是将数组指针指向数组首
while(list($key,$value)=each($speed)){
echo