PHP合并數組+號和array_merge的區別_PHP教程

來源:互聯網
上載者:User

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)

http://www.bkjia.com/PHPjc/1021557.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1021557.htmlTechArticlePHP合并數組+號和array_merge的區別 這篇文章主要介紹了PHP合并數組+號和array_merge的區別,PHP的數組融合一般有兩種做法,一種是直接使用加號相...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.