1,增添数组成员
例1,$num[] = value直接赋值追加到数组末尾:
<?PHP $num = array(1=>80,2=>120,3=>160); echo "使用表达式添加数组成员<br />"; $num[]=240; print_r($num); //by www. ?>
运行结果:
使用表达式添加数组成员
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. ?>
运行结果:
使用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. ?>
运行结果:
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. ?>
运行结果:
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. ?>
运行结果:(运行出错及说明数组也被删除,不再存在)
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:\wamp\www\testing\editorplus\test.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. ?>
例3,array_unique删除数组中的重复值:
<?php $a=array("red", "green", "blue", "yellow","blue","green"); $result = array_unique($a); print_r($result); //by www. ?>
运行结果:
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. $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数组的增、删、改操作,就介绍这些了,希望可以帮助到大家。
php数组的其它用法,主要了解以下php数组函数的用途。
cout($array) --------统计数组的单元个数
array_diff($array1,$array2)----------统计数组之间的不同点,返回第一个数组中有而第二个数组中没有的。
array_diff_assoc($array1,$array2)---------同array_diff(),只是它对键值也比较
array_diff_key($array1,$array2)------------比较键值
array_product($array)-----------返回数组的所有数的乘积
array_sum($array)--------------所有数值的和
array_rand($array,$n)----------在$array数组中取出$n个数值,返回数组
array_intersect($array1,$array2)-----------取得两个数组的交集
array_intersect_assoc($array1,$array2)--------在array_intersect 的基础上进行键值比较
array_intersect_key($array1,$array2)----------比较两个数组键值的交集
在php编程中,对数组元素进行填充,可以使用array_fill()函数。
来看下面的例子,array_fill()函数——填充数组函数:
<?PHP //数组填充 //array_fill()例子 //by www. $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
)