";
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>";
?>
运行结果:
姓名 |
性别 |
年龄 |
小张 |
男 |
24 |
小王 |
女 |
25 |
小李 |
男 |
23 |
3.for循环遍历:
PHP $speed = range(0,220,20);
for($i =0;$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,20);
echo current($speed);//
输出当前位置的值(在数组的开头位置)
$i = rand(1,11);
while($i--){
next($speed);//指针从当前位置向后移动一位
}
echo current($speed);//
输出当前位置的值
echo "
";
echo prev($speed);//
输出前一位置数组值
echo "
";
echo reset($speed);//重置数组的指针,将指针指向起始位置
echo "
";
echo end($speed);//
输出最后位置的数组值
echo "
";
?>
运行结果:
0220
200
0
220
实例二:each函数指针操作
PHP $speed = range(0,200,40);
echo "each实现指针下移
";
echo "0挡的速度是".current(each($speed))."
";
echo "1挡的速度是".current(each($speed))."
";
echo "2挡的速度是".current(each($speed))."
";
echo "3挡的速度是".current(each($speed))."
";
echo "4挡的速度是".current(each($speed))."
";
echo "5挡的速度是".current(each($speed))."
";
echo "使用each
函数实现数组指针的移动,进行数组遍历
";
reset($speed);//这里是将数组指针指向数组首
while(list($key,$value)=each($speed)){
echo $key."=>".$value."
";
}
?>
运行结果:
each实现指针下移
0挡的速度是0
1挡的速度是40
2挡的速度是80
3挡的速度是120
4挡的速度是160
5挡的速度是200
使用each
函数实现数组指针的移动,进行数组遍历
0=>0
1=>40
2=>80
3=>120
4=>160
5=>200
五、数组的增添删改操作:
1.增添数组成员:实例一:$num[] = value直接赋值追加到数组末尾:
PHP $num = array(1=>80,2=>120,3=>160);
echo "使用表达式
添加数组成员
";
$num[]=240;
print_r($num);
?>
运行结果:
使用表达式添加数组成员Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )
实例二:array_pad函数,数组数组首尾选择性追加
PHP $num = array(1=>80,3=>160);
$num = array_pad($num,4,200);
echo "使用array_pad
函数向数组尾部
添加成员
";
print_r($num);
echo "
array_pad 还可以填充数组首部
";
$num = array_pad($num,-8,40);
print_r($num);
?>
运行结果:
使用array_pad
函数向数组尾部
添加成员Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 200 ) array_pad 还可以填充数组首部Array ( [0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5] => 120 [6] => 160 [7] => 200 )
实例三:入栈操作追加(array_push):
PHP $num = array(1=>80,3=>160);
array_push($num,280);//可以自己追加,直接加在数组结尾
print_r($num);
?>
运行结果:
Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )
实例四:array_unshift()在开头添加数组成员
PHP $num = array(1=>80,3=>160);
array_unshift($num,40);//可以自己追加,直接加在数组结尾
print_r($num);
?>
运行结果:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
注意:array_unshift()函数使用后数组的键值将会从0开始!
2.删减数组成员:
实例一:unset()命令删除数组成员或数组:
PHP $num = array_fill(0,rand(1,10));
print_r($num);
echo "
";
unset($num[4]);
print_r($num);
echo "
";
unset($num);
if(is_array){
echo "unset命令不能
删除整个数组";
}else{
echo "unset命令可以
删除数组";
}
?>
运行结果:(运行出错及说明数组也被删除,不再存在)
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 [4] => 9 )
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 )
Notice: Use of undefined constant is_array - assumed 'is_array' in H:\wamp\www\testing\editorplus\test.
PHP on line 21
unset命令不能
删除整个数组
实例二:array_splice()函数删除数组成员
PHP $a=array("red","green","yellow");
count ($a); //得到4
array_splice($a,1,1); //
删除第二个元素
count ($a); //得到3
echo $a[2]; //得到yellow
echo $a[1]; //得到blue
?>
实例三:array_unique删除数组中的重复值:
PHP $a=array("red","yellow","green");
$result = array_unique($a);
print_r($result);
?>
运行结果:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )
实例四:array_merge、array_merge_recursive合并数组
PHP $array1 = array("r"=>"red",2,4);
$array2 = array("b"=>"blue",4=>5,6,8,9);
$array3 = array("r"=>"read",4=>10,2=>11);
$array4 = array(
array(4=>10),
array(7=>13)
);
$array5 = array(
array(4=>11),
array(6=>12)
);
$result = array_merge($array1,$array2,$array3,$array4,$array5);
echo "
";
print_r($result);
echo "
";
$result = array_merge_recursive($array1,$array5);
echo "
";
print_r ($result);
echo "
";
?>
运行结果:
Array
(
[r] => read
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[b] => blue
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => Array
(
[4] => 10
)
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
Array
(
[r] => Array
(
[0] => red
[1] => read
)
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[b] => blue
[4] => 5
[5] => 6
[6] => 7
[7] => 8
[8] => 9
[9] => 10
[10] => 11
[11] => Array
(
[4] => 10
)
[12] => Array
(
[7] => 13
)
[13] => Array
(
[4] => 11
)
[14] => Array
(
[6] => 12
)
)
注:1.array_merge的键名是数字的将重新建立索引;遇到相同的字符串键名时,后面的将覆盖前面的。
2.array_merge_recursive函数的作用是将相同字符串的键名单元整合成一个数组。
六、数组的键值和值操作:
实例一:in_array()检测数组中是否有某个值存在
PHP $array = range(0,9);
if(in_array(9,$array)){
echo "数组中存在";
}
?>
运行结果:
数组中存在
实例二:key()取得数组当前的键名:
PHP $array = range(0,9);
$num = rand(0,8);
while($num--)
next($array);
$key = key($array);
echo $key;
?>
此实例结果为动态结果,范围(0-8),不做结果演示。
实例三:list()函数把数组中的值赋给指定变量:
PHP $staff = array(
array("姓名",$age) = $value;
echo "
}
echo "
?> PHP $array = array("red","Black");
print_r($array);
echo "
";
$array = array_flip($array);
print_r($array);
?>
3013">
PHP $array = array("red","Black");
$result = array_keys($array);
print_r($result);
echo "
";
$result = array_values($array);
print_r($result);
?>
PHP $array = array("red","Black");
$result = array_search("red",$array);
if(($result === NULL)){
echo "不存在数值red";
}else{
echo "存在数值 $result";
}
?>
PHP $array = array("b","c","d","a");
sort($array);//从低到高排序
print_r($array);
echo "
";
rsort($array);//逆向排序
print_r($array);
?>
PHP $array = array("a","b","d");
shuffle($array);//从低到高排序
print_r($array);
?>
PHP $array = array("d","a","c");
$array = array_reverse($array);//从低到高排序
print_r($array);
?>
PHP $array = array("sort2","Sort5","sort1","sort4");
natsort($array);//从低到高排序
print_r($array);
echo "
";
natcasesort($array);
print_r($array);
?>
PHP $array = array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4");
ksort($array);//从低到高排序
print_r($array);
?>