首頁 >後端開發 >php教程 >PHP合并数组+号和array_merge的区别_PHP教程

PHP合并数组+号和array_merge的区别_PHP教程

WBOY
WBOY原創
2016-07-13 09:48:381025瀏覽

PHP合并数组+号和array_merge的区别

   这篇文章主要介绍了PHP合并数组+号和array_merge的区别,PHP的数组融合一般有两种做法,一种是直接使用加号相加,另一种则是使用array_merge函数进行相加,两者之间有点区别,需要的朋友可以参考下

  PHP的数组融合一般有两种做法,一种是直接使用加号相加,另一种则是使用array_merge函数进行相加,两者之间有点区别:

  1.相加会证数组中的自然index不被重置

  2.相加方法中,被加的数组内的值不会被覆盖

  3.merge函数中的自然index会被重置

  4.merge函数,无所谓被merge和merge关系,越后面的array参数,其值,会覆盖较前面的array参数相同键的值

  例子:

  数组1:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

$arr1 = array(

10 => 'valueof10',

11 => 'valueof11',

'key1' => 'dataofkey1',

'key2' => 'dataofkey2',

'array' => array(

'key1' => 1,

'key2' => 'abc',

'key3' => array(

1, 2, 'name' => 'peter'

),

),

);

  数组2:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

$arr2 = array(

10 => 'newvalueof10',

11 => 'newvalueof11',

'key1' => 'newdataofkey1',

'key3' => 'newdataofkey3',

'array' => array(

'key1' => 2,

'key2' => 'defg',

'key3' => array(

1, 2, 'name' => 'jonathan', 'gender' => 'male'

),

),

);

  结果比较:

  ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

array (size=6)

10 => string 'valueof10' (length=9)

11 => string 'valueof11' (length=9)

'key1' => string 'dataofkey1' (length=10)

'key2' => string 'dataofkey2' (length=10)

'array' =>

array (size=3)

'key1' => int 1

'key2' => string 'abc' (length=3)

'key3' =>

array (size=3)

0 => int 1

1 => int 2

'name' => string 'peter' (length=5)

'key3' => string 'newdataofkey3' (length=13)

 

array (size=8)

0 => string 'valueof10' (length=9)

1 => string 'valueof11' (length=9)

'key1' => string 'newdataofkey1' (length=13)

'key2' => string 'dataofkey2' (length=10)

'array' =>

array (size=3)

'key1' => int 2

'key2' => string 'defg' (length=4)

'key3' =>

array (size=4)

0 => int 1

1 => int 2

'name' => string 'jonathan' (length=8)

'gender' => string 'male' (length=4)

2 => string 'newvalueof10' (length=12)

3 => string 'newvalueof11' (length=12)

'key3' => string 'newdataofkey3' (length=13)

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1021557.htmlTechArticlePHP合并数组+号和array_merge的区别 这篇文章主要介绍了PHP合并数组+号和array_merge的区别,PHP的数组融合一般有两种做法,一种是直接使用加号相...
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn