php数组实例之数组的增删改

发布时间:2020-08-31编辑:脚本学堂
本文介绍下,有关php数组入门之数组的增、删除、改的相关内容,有需要的朋友参考下。

1,增添数组成员
例1,$num[] = value直接赋值追加到数组末尾:
 

<?PHP
$num = array(1=>80,2=>120,3=>160);
echo "使用表达式添加数组成员<br />";
$num[]=240;
print_r($num);
//by www.jb200.com
?>

运行结果:
使用表达式添加数组成员
Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )

例2,array_pad函数,数组数组首尾选择性追加。
 

<?PHP
$num = array(1=>80,2=>120,3=>160);
$num = array_pad($num,4,200);
echo "使用array_pad函数向数组尾部添加成员<br />";
print_r($num);
echo "<br />array_pad 还可以填充数组首部<br />";
$num = array_pad($num,-8,40);
print_r($num);
//by www.jb200.com
?>

运行结果:
使用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 )

例3,入栈操作追加(array_push):
 

<?PHP
$num = array(1=>80,2=>120,3=>160);
array_push($num,200,240,280);//可以自己追加,直接加在数组结尾
print_r($num);//by www.jb200.com
?>

运行结果:
Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )

例4,array_unshift()在开头添加数组成员
 

<?PHP
$num = array(1=>80,2=>120,3=>160);
array_unshift($num,0,40);//可以自己追加,直接加在数组结尾
print_r($num);
//by www.jb200.com
?>

运行结果:
Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )
注意:array_unshift()函数使用后数组的键值将会从0开始!

2,删减数组成员
例1,unset()命令删除数组成员或数组:
 

<?PHP
$num = array_fill(0,5,rand(1,10));
print_r($num);
echo "<br />";
unset($num[4]);
print_r($num);
echo "<br />";
unset($num);
if(is_array){
echo "unset命令不能删除整个数组";
}else{
echo "unset命令可以删除数组";
} //by www.jb200.com
?>

运行结果:(运行出错及说明数组也被删除,不再存在)
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 [4] => 9 )
Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 )

说明:
Use of undefined constant is_array - assumed 'is_array' in H:wampwwwtestingeditorplustest.php on line 21
unset命令不能删除整个数组。

例2,array_splice()函数删除数组成员
 

<?php
$a=array("red", "green", "blue", "yellow");
count ($a); //得到4
array_splice($a,1,1); //删除第二个元素
count ($a); //得到3
echo $a[2]; //得到yellow
echo $a[1]; //得到blue
//by www.jb200.com
?>

例3,array_unique删除数组中的重复值:
 

<?php
$a=array("red", "green", "blue", "yellow","blue","green");
$result = array_unique($a);
print_r($result);
//by www.jb200.com
?>

运行结果:
Array ( [0] => red [1] => green [2] => blue [3] => yellow )

例4,array_merge、array_merge_recursive合并数组
 

<?php
$array1 = array("r"=>"red",1,2,3,4);
$array2 = array("b"=>"blue",4=>5,6,7,8,9);
$array3 = array("r"=>"read",4=>10,2=>11);
$array4 = array(
array(4=>10),
array(7=>13)
); //by www.jb200.com
$array5 = array(
array(4=>11),
array(6=>12)
);
$result = array_merge($array1,$array2,$array3,$array4,$array5);
echo "<pre>";
print_r($result);
echo "</pre>";
$result = array_merge_recursive($array1,$array2,$array3,$array4,$array5);
echo "<pre>";
print_r ($result);
echo "</pre>";
?>

运行结果:
 

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函数的作用是将相同字符串的键名单元整合成一个数组。

有关php数组的增、删、改操作,就介绍这些了,希望可以帮助到大家。