In PHP, array_merge merges two arrays.

Source: Internet
Author: User
In php, we use the array_merge () function to merge data. the array_merge () function combines two or more numbers into an array. If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ).

In php, we use the array_merge () function to merge data. the array_merge () function combines two or more numbers into an array. if the key name already exists, the key value of the key is the value corresponding to the last key name (the latter overwrites the previous one ). if the array is a digital index, the key name is re-indexed continuously.

The instance code is as follows:

  1. Echo "rn First case rn ";
  2. $ A = array (1, 2, 3, 4, 5, 6 );
  3. $ B = array (7,8, 9 );
  4. $ C = array_merge ($ a, $ B );
  5. Print_r ($ c );
  6. $ C = $ a + $ B;
  7. Print_r ($ c );
  8. $ C = $ B + $;
  9. Print_r ($ c );
  10. Echo "rn second case rn ";
  11. $ A = array ('A', 'B', 'C', 'D', 'e', 'F ');
  12. $ B = array ('A', 'X', 'y ');
  13. $ C = array_merge ($ a, $ B );
  14. Print_r ($ c );
  15. $ C = $ a + $ B;
  16. Print_r ($ c );
  17. $ C = $ B + $;
  18. Print_r ($ c );
  19. Echo "rn case 3 rn ";
  20. $ A = array (
  21. 1 => 'A ',
  22. 2 => 'B ',
  23. 3 => 'C ',
  24. 4 => 'D ',
  25. 5 => 'e ',
  26. 6 => 'F ');
  27. $ B = array (
  28. 1 => 'A ',
  29. 7 => 'X ',
  30. 8 => 'y ');
  31. $ C = array_merge ($ a, $ B );
  32. Print_r ($ c );
  33. $ C = $ a + $ B;
  34. Print_r ($ c );
  35. $ C = $ B + $;
  36. Print_r ($ c );
  37. ?>
  38. The result is as follows:
  39. First case
  40. Array
  41. (
  42. [0] => 1
  43. [1] => 2
  44. [2] => 3
  45. [3] => 4
  46. [4] => 5
  47. [5] => 6
  48. [6] => 7
  49. [7] => 8
  50. [8] => 9
  51. )
  52. Array
  53. (
  54. [0] => 1
  55. [1] => 2
  56. [2] => 3
  57. [3] => 4
  58. [4] => 5
  59. [5] => 6
  60. )
  61. Array
  62. (
  63. [0] => 7
  64. [1] => 8
  65. [2] => 9
  66. [3] => 4
  67. [4] => 5
  68. [5] => 6
  69. )
  70. Case 2
  71. Array
  72. (
  73. [0] =>
  74. [1] => B
  75. [2] => c
  76. [3] => d
  77. [4] => e
  78. [5] => f
  79. [6] =>
  80. [7] => x
  81. [8] => y
  82. )
  83. Array
  84. (
  85. [0] =>
  86. [1] => B
  87. [2] => c
  88. [3] => d
  89. [4] => e
  90. [5] => f
  91. )
  92. Array
  93. (
  94. [0] =>
  95. [1] => x
  96. [2] => y
  97. [3] => d
  98. [4] => e
  99. [5] => f
  100. )
  101. Case 3
  102. Array
  103. (
  104. [0] =>
  105. [1] => B
  106. [2] => c
  107. [3] => d
  108. [4] => e
  109. [5] => f
  110. [6] =>
  111. [7] => x
  112. [8] => y
  113. )
  114. Array
  115. (
  116. [1] =>
  117. [2] => B
  118. [3] => c
  119. [4] => d
  120. [5] => e
  121. [6] => f
  122. [7] => x
  123. [8] => y
  124. )
  125. Array
  126. (
  127. [1] =>
  128. [7] => x
  129. [8] => y
  130. [2] => B
  131. [3] => c
  132. [4] => d
  133. [5] => e
  134. [6] => f
  135. )

1) when the key name is a number, array_merge () will not overwrite the original value, but + merges the array and returns the first value as the final result, the values of the following arrays with the same key names are discarded (not overwritten)

2) when the key name is a character, + still returns the first value as the final result, and discards the values with the same key name in the subsequent array, but array_merge () at this time, the values with the same key names will be overwritten.

Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.