PHP

14个成员

PHP数组各种操作与函数汇总

发表于 2017-01-17 4131 次查看

对于Web编程来说,最重要的就是存取和读写数据了。存储方式可能有很多种,可以是字符串、数组、文件的形式等。数组,可以说是PHP的数据应用中较重要的一种方式。PHP的数组函数众多,下面是我学习的小结,借此记之,便于以后鉴之。

    1. 数组定义

数组的定义使用 array()方式定义,可以定义空数组:

01<?php

02 $number = array(1,3,5,7,9);

03 //定义空数组

04 $result = array();

05 $color =array("red","blue","green");

06 //自定义键值

07 $language = (1=>"English",3=>"Chinese",5=>"Franch");

08 //定义二维数组

09 $two = array(

10 "color"=>array("red","blue"), //用逗号结尾

11 "week"=>array("Monday","Friday") //最后一句没有标点

12 );

13?>

    2. 创建数组

compact()

compact()函数——将一个或多个变量(包含数组)转换为数组:array compact ( mixed $varname [, mixed $... ] )。

1<?PHP

2 $number = "1,3,5,7,9";

3 $string = "I'm PHPer";

4 $array = array("And","You?");

5 $newArray = compact("number","string","array");

6 print_r ($newArray);

7?>

   

compact()函数用于将两个或多个变量转换为数组,当然也包含数组变量。其参数是变量的名称而非带有$全名。相反的函数是extract()作用顾名思义就是将数组转换为单个的字符串,键值作为其字符串名称,数组值作为字符串的值。

       

运行结果:

1Array (

2 [number] => 1,3,5,7,9

3 [string] => I'm PHPer

4 [array] => Array ( [0] => And [1] => You? )

5)

   

array_combine()

       

array_combine()——将两个数组重组成一个数组,一个作键值一个做的值:array array_combine ( array $keys , array $values )

1<?PHP

2 $number = array("1","3","5","7","9");

3 $array = array("I","Am","A","PHP","er");

4 $newArray = array_combine($number,$array);

5 print_r ($newArray);

6?>

array_combine函数不多说了,谁看了都明白。

   

运行结果:

1Array ( [1] => I [3] => Am [5] => A [7] => PHP [9] => er )

       

range()

   

range()函数——创建指定范围的数组:

01<?PHP

02 $array1 = range(0,100,10);//0为起始值,100为结束值,10为步进值(默认步进值为1).

03 print_r($array1);

04 echo"<br />";

05 $array2 = range("A","Z");

06 print_r($array2);

07 echo "<br />";

08 $array3 = range("z","a");

09 print_r($array3);

10?>

       

array_fill()

array_fill()函数——填充数组函数:

01<?PHP

02 $array = range(1,10);

03 $fillarray = range("a","d");

04 $arrayFilled = array_fill(0,5,$fillarray);//这里的$fillarray可以是字符串,如"test".

05 echo "<pre>";

06 print_r ($arrayFilled);

07 echo "</pre>";

08 $keys = array("string","2",9,"SDK","PK");

09 $array2 = array_fill_keys($keys,"testing");

10 echo "<pre>";

11 print_r ($array2);

12 echo "</pre>";

13?>

运行结果:

01Array

02(

03 [0] => Array

04 (

05 [0] => a

06 [1] => b

07 [2] => c

08 [3] => d

09 )

10 [1] => Array

11 (

12 [0] => a

13 [1] => b

14 [2] => c

15 [3] => d

16 )

17 [2] => Array

18 (

19 [0] => a

20 [1] => b

21 [2] => c

22 [3] => d

23 )

24 [3] => Array

25 (

26 [0] => a

27 [1] => b

28 [2] => c

29 [3] => d

30 )

31 [4] => Array

32 (

33 [0] => a

34 [1] => b

35 [2] => c

36 [3] => d

37 )

38)

39Array

40(

41 [string] => testing

42 [2] => testing

43 [9] => testing

44 [SDK] => testing

45 [PK] => testing

46)

    3. 数组的遍历

foreach遍历

   

foreach (array_expression as $value){}

       

foreach (array_expression as $key => $value){}

1<?PHP

2 $speed = array(50,120,180,240,380);

3 foreach($speed as $keys=>$values){

4 echo $keys."=>".$values."<br />";

5 }

6?>

运行结果:

10=>50

21=>120

32=>180

43=>240

54=>380

while循环遍历

   

while循环遍历一般结合list函数,以下是实例

01<?PHP

02 $staff = array(

03 array("姓名","性别","年龄"),

04 array("小张","男",24),

05 array("小王","女",25),

06 array("小李","男",23)

07 );

08 echo "<table border=2>";

09 while(list($keys,$value) = each($staff)){

10 list($name,$sex,$age) = $value;

11 echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";

12 }

13 echo "</table>";

14 ?>

       

for循环遍历

1<?PHP

2 $speed = range(0,220,20);

3 for($i =0;$i<count($speed);$i++) {

4 echo $speed[$i]." ";

5 }

6?>

   

运行结果:

10 20 40 60 80 100 120 140 160 180 200 220

       4. 数组的指针操作

涉及函数包括reset、prev、end、next、current、each。

实例一:next 与 prev

01<?PHP

02 $speed = range(0,220,20);

03 echo current($speed);//输出当前位置的值(在数组的开头位置)

04 $i = rand(1,11);

05 while($i--){

06 next($speed);//指针从当前位置向后移动一位

07 }

08 echo current($speed);//输出当前位置的值

09 echo "<br />";

10 echo prev($speed);//输出前一位置数组值

11 echo "<br />";

12 echo reset($speed);//重置数组的指针,将指针指向起始位置

13 echo "<br />";

14 echo end($speed);//输出最后位置的数组值

15 echo "<br />";

16?>

       

运行结果:

10220

2200

30

4220

   

实例二:each函数指针操作

01<?PHP

02 $speed = range(0,200,40);

03 echo "each实现指针下移 <br />";

04 echo "0挡的速度是".current(each($speed))."<br />";

05 echo "1挡的速度是".current(each($speed))."<br />";

06 echo "2挡的速度是".current(each($speed))."<br />";

07 echo "3挡的速度是".current(each($speed))."<br />";

08 echo "4挡的速度是".current(each($speed))."<br />";

09 echo "5挡的速度是".current(each($speed))."<br />";

10 echo "使用each函数实现数组指针的移动,进行数组遍历 <br />";

11 reset($speed);//这里是将数组指针指向数组首

12 while(list($key,$value)=each($speed)){

13 echo $key."=>".$value."<br />";

14 }

15?>

       

运行结果:

01each实现指针下移

020挡的速度是0

031挡的速度是40

042挡的速度是80

053挡的速度是120

064挡的速度是160

075挡的速度是200

08使用each函数实现数组指针的移动,进行数组遍历

090=>0

101=>40

112=>80

123=>120

134=>160

145=>200

5. 数组的增添删改操作

增添数组成员

   

实例一:$num[] = value直接赋值追加到数组末尾:

1<?PHP

2 $num = array(1=>80,2=>120,3=>160);

3 echo "使用表达式添加数组成员<br />";

4 $num[]=240;

5 print_r($num);

6 ?>

       

运行结果:

1使用表达式添加数组成员

2Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 240 )

实例二:array_pad函数,数组数组首尾选择性追加

1<?PHP

2 $num = array(1=>80,2=>120,3=>160);

3 $num = array_pad($num,4,200);

4 echo "使用array_pad函数向数组尾部添加成员<br />";

5 print_r($num);

6 echo "<br />array_pad 还可以填充数组首部<br />";

7 $num = array_pad($num,-8,40);

8 print_r($num);

9?>

       

运行结果:

1使用array_pad函数向数组尾部添加成员

2Array ( [0] => 80 [1] => 120 [2] => 160 [3] => 200 )

3array_pad 还可以填充数组首部

4Array ( [0] => 40 [1] => 40 [2] => 40 [3] => 40 [4] => 80 [5] => 120 [6] => 160 [7] => 200 )

   

实例三:入栈操作追加(array_push):

1<?PHP

2 $num = array(1=>80,2=>120,3=>160);

3 array_push($num,200,240,280);//可以自己追加,直接加在数组结尾

4 print_r($num);

5?>

       

运行结果:

1Array ( [1] => 80 [2] => 120 [3] => 160 [4] => 200 [5] => 240 [6] => 280 )

实例四:array_unshift()在开头添加数组成员

1<?PHP

2 $num = array(1=>80,2=>120,3=>160);

3 array_unshift($num,0,40);//可以自己追加,直接加在数组结尾

4 print_r($num);

5?>

       

运行结果:

1Array ( [0] => 0 [1] => 40 [2] => 80 [3] => 120 [4] => 160 )

   

注意:array_unshift()函数使用后数组的键值将会从0开始!

       

删减数组成员

实例一:unset()命令删除数组成员或数组:

01<?PHP

02 $num = array_fill(0,5,rand(1,10));

03 print_r($num);

04 echo "<br />";

05 unset($num[4]);

06 print_r($num);

07 echo "<br />";

08 unset($num);

09 if(is_array){

10 echo "unset命令不能删除整个数组";

11 }else{

12 echo "unset命令可以删除数组";

13 }

14?>

       

运行结果:(运行出错及说明数组也被删除,不再存在)

1Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 [4] => 9 )

2Array ( [0] => 9 [1] => 9 [2] => 9 [3] => 9 )

3Notice: Use of undefined constant is_array - assumed 'is_array' in H:\wamp\www\testing\editorplus\test.php on line 21

4unset命令不能删除整个数组

实例二:array_splice()函数删除数组成员

1<?php

2 $a=array("red", "green", "blue", "yellow");

3 count ($a); //得到4

4 array_splice($a,1,1); //删除第二个元素

5 count ($a); //得到3

6 echo $a[2]; //得到yellow

7 echo $a[1]; //得到blue

8?>

       

实例三:array_unique删除数组中的重复值:

1<?php

2 $a=array("red", "green", "blue", "yellow","blue","green");

3 $result = array_unique($a);

4 print_r($result);

5?>

   

运行结果:

1Array ( [0] => red [1] => green [2] => blue [3] => yellow )

       

实例四:array_merge、array_merge_recursive合并数组

01<?php

02 $array1 = array("r"=>"red",1,2,3,4);

03 $array2 = array("b"=>"blue",4=>5,6,7,8,9);

04 $array3 = array("r"=>"read",4=>10,2=>11);

05 $array4 = array(

06 array(4=>10),

07 array(7=>13)

08 );

09 $array5 = array(

10 array(4=>11),

11 array(6=>12)

12 );

13 $result = array_merge($array1,$array2,$array3,$array4,$array5);

14 echo "<pre>";

15 print_r($result);

16 echo "</pre>";

17 $result = array_merge_recursive($array1,$array2,$array3,$array4,$array5);

18 echo "<pre>";

19 print_r ($result);

20 echo "</pre>";

21?>

运行结果:

01Array

02(

03 [r] => read

04 [0] => 1

05 [1] => 2

06 [2] => 3

07 [3] => 4

08 [b] => blue

09 [4] => 5

10 [5] => 6

11 [6] => 7

12 [7] => 8

13 [8] => 9

14 [9] => 10

15 [10] => 11

16 [11] => Array

17 (

18 [4] => 10

19 )

20 [12] => Array

21 (

22 [7] => 13

23 )

24 [13] => Array

25 (

26 [4] => 11

27 )

28 [14] => Array

29 (

30 [6] => 12

31 )

32)

33Array

34(

35 [r] => Array

36 (

37 [0] => red

38 [1] => read

39 )

40 [0] => 1

41 [1] => 2

42 [2] => 3

43 [3] => 4

44 [b] => blue

45 [4] => 5

46 [5] => 6

47 [6] => 7

48 [7] => 8

49 [8] => 9

50 [9] => 10

51 [10] => 11

52 [11] => Array

53 (

54 [4] => 10

55 )

56 [12] => Array

57 (

58 [7] => 13

59 )

60 [13] => Array

61 (

62 [4] => 11

63 )

64 [14] => Array

65 (

66 [6] => 12

67 )

68)

       

注:1. array_merge的键名是数字的将重新建立索引;遇到相同的字符串键名时,后面的将覆盖前面的。 2. array_merge_recursive函数的作用是将相同字符串的键名单元整合成一个数组。

6. 数组的键值和值操作

实例一:in_array()检测数组中是否有某个值存在

1<?php

2 $array = range(0,9);

3 if(in_array(9,$array)){

4 echo "数组中存在";

5 }

6?>

   

运行结果:数组中存在

       

实例二:key()取得数组当前的键名:

1<?php

2 $array = range(0,9);

3 $num = rand(0,8);

4 while($num--)

5 next($array);

6 $key = key($array);

7 echo $key;

8?>

此实例结果为动态结果,范围(0-8),不做结果演示。

实例三:list()函数把数组中的值赋给指定变量:

01<?PHP

02 $staff = array(

03 array("姓名","性别","年龄"),

04 array("小张","男",24),

05 array("小王","女",25),

06 array("小李","男",23)

07 );

08 echo "<table border=2>";

09 while(list($keys,$value) = each($staff)){

10 list($name,$sex,$age) = $value;

11 echo "<tr><td>$name</td><td>$sex</td><td>$age</td></tr>";

12 }

13 echo "</table>";

14?>

       

实例四:array_flip()交换数组的键值和值:

1<?PHP

2 $array = array("red","blue","yellow","Black");

3 print_r($array);

4 echo "<br />";

5 $array = array_flip($array);

6 print_r($array);

7 ?>

   

运行结果:

1Array ( [0] => red [1] => blue [2] => yellow [3] => Black )

2Array ( [red] => 0 [blue] => 1 [yellow] => 2 [Black] => 3 )

       

实例五:array_keys()、array_values()返回数组中所有的键值和值:

1<?PHP

2 $array = array("red","blue","yellow","Black");

3 $result = array_keys($array);

4 print_r($result);

5 echo "<br />";

6 $result = array_values($array);

7 print_r($result);

8 ?>

运行结果:

1Array ( [0] => 0 [1] => 1 [2] => 2 [3] => 3 )

2Array ( [0] => red [1] => blue [2] => yellow [3] => Black )

实例六:array_search()搜索数值:

1<?PHP

2 $array = array("red","blue","yellow","Black");

3 $result = array_search("red",$array);

4 if(($result === NULL)){

5 echo "不存在数值red";

6 }else{

7 echo "存在数值 $result";

8 }

9 ?>

       

结果:存在数值 0

   

函数array_search()返回的值可能为false或0或NULL,所以在判断时注意要用"==="

       7. 数组的排序

实例一:sort()、rsort()/asort()、arsort()对数组排序:

1<?PHP

2 $array = array("b","c","d","a");

3 sort($array);//从低到高排序

4 print_r($array);

5 echo "<br />";

6 rsort($array);//逆向排序

7 print_r($array);

8?>

结果:

1Array ( [0] => a [1] => b [2] => c [3] => d )

2Array ( [0] => d [1] => c [2] => b [3] => a )

       

sort()、rsort()函数对数组进行从低到高的排序,返回结果为bool值;

   

asort()、arsort()函数是保留键值的排序,排序后键值不重新索引。

       

实例二:将数组顺序打乱——shuffle()函数:

1<?PHP

2 $array = array("a","b","c","d");

3 shuffle($array);//从低到高排序

4 print_r($array);

5?>

结果为动态结果:

1Array ( [0] => c [1] => a [2] => d [3] => b )

shuffle的结果有点随机的意味,每次刷新都不一样。

       

实例三:array_reverse()数组反向:

1<?PHP

2 $array = array("d","b","a","c");

3 $array = array_reverse($array);//从低到高排序

4 print_r($array);

5?>

   

运行结果:

1Array ( [0] => c [1] => a [2] => b [3] => d )

       

实例四:自然排序算法——natsort()和natcasesort();

1<?PHP

2 $array = array("sort2","Sort5","sort1","sort4");

3 natsort($array);//从低到高排序

4 print_r($array);

5 echo "<br />";

6 natcasesort($array);

7 print_r($array);

8?>

结果:

1Array ( [1] => Sort5 [2] => sort1 [0] => sort2 [3] => sort4 )

2Array ( [2] => sort1 [0] => sort2 [3] => sort4 [1] => Sort5 )

natsort()、natcasesort()对数组进行自然排序,就是使用数字的正常排序算法。natcasesort会忽略大小写。

       

实例五:对数组进行键值排序ksort():

1<?PHP

2 $array = array(1=>"sort2",4=>"Sort5",2=>"sort1",3=>"sort4");

3 ksort($array);//从低到高排序

4 print_r($array);

5?>

   

结果:

1Array ( [1] => sort2 [2] => sort1 [3] => sort4 [4] => Sort5 )

       

注意:ksort()函数重新建立了索引。

8. 数组的其他用法

01cout($array) --------统计数组的单元个数

02array_diff($array1,$array2)----------统计数组之间的不同点,返回第一个数组中有而第二个数组中没有的。

03array_diff_assoc($array1,$array2)---------同array_diff(),只是它对键值也比较

04array_diff_key($array1,$array2)------------比较键值

05array_product($array)-----------返回数组的所有数的乘积

06array_sum($array)--------------所有数值的和

07array_rand($array,$n)----------在$array数组中取出$n个数值,返回数组

08array_intersect($array1,$array2)----------------取得两个数组的交集

09array_intersect_assoc($array1,$array2)---------------在array_intersect 的基础上进行键值比较

10array_intersect_key($array1,$array2)-----------------比较两个数组键值的交集

       总结

数组的使用在PHP中至关重要,由于PHP没有指针,所以数组承担了很大的数据操作任务。学好数组,才能把PHP应用的得心应手,这里所列均是常用的PHP数组相关的函数及用法,欢迎一起学习!
 

发表回复
你还没有登录,请先登录注册