Home > Backend Development > PHP Tutorial > The difference between PHP merge array number and array_merge_PHP tutorial

The difference between PHP merge array number and array_merge_PHP tutorial

WBOY
Release: 2016-07-13 09:48:38
Original
1023 people have browsed it

The difference between PHP merging array numbers and array_merge

This article mainly introduces the difference between PHP merging array numbers and array_merge. There are generally two methods for PHP array fusion. One is One is to add directly using the plus sign, and the other is to use the array_merge function to add. There are some differences between the two. Friends in need can refer to it

There are generally two methods of array fusion in PHP. One is to add directly using the plus sign, and the other is to use the array_merge function to add. There is a slight difference between the two:

1. The natural index in the addition verification array will not be reset

2. In the addition method, the values ​​in the added array will not be overwritten

 3. The natural index in the merge function will be reset

4. The merge function does not matter the relationship between merged and merged. The value of the later array parameter will overwrite the value of the same key of the earlier array parameter

Example:

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'

),

),

);

1

2

3

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'

),

),

);

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' ), ), );
Array 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' ), ), );

Results comparison:

 ?

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)

1 2

34 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)
http://www.bkjia.com/PHPjc/1021557.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1021557.htmlTechArticleThe difference between PHP merging array numbers and array_merge This article mainly introduces the difference between PHP merging array numbers and array_merge, PHP There are generally two ways to fuse arrays. One is to directly use the plus sign...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template