PHP merges arrays + and array_merge

Source: Internet
Author: User

You can use + or array_merge to merge two arrays in PHP, but there are still differences between the two. It is necessary to clearly understand the differences in the two Processing Methods for rapid development of the project.
 
The main difference is that if the same key name appears in two or more arrays, pay attention to the following two points:

First, you must note that the array key names in php can be divided into strings (associated arrays) or numbers (numeric arrays). Here we will not discuss multi-dimensional arrays.
 
(1) When the key name is a number (value array), array_merge () does not overwrite the original value, but + merges the array to return 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 (associated array), + returns the first value as the final result, and discards the values with the same key name in the subsequent array, however, array_merge () overwrites the values with the same key name.

The following are examples:

M: Array (
[0] =>
[1] => B
)
N: Array (
[0] => c
[1] => d
)
M + n: Array (
[0] =>
[1] => B
)
The result of array_merge (m, n) is: Array (
[0] =>
[1] => B
[2] => c
[3] => d
)
M: Array (
[1] =>
[2] => B
)
N: Array (
[2] => c
[3] => d
)
M + n: Array (
[1] =>
[2] => B
[3] => d
)
The result of array_merge (m, n) is: Array (
[0] =>
[1] => B
[2] => c
[3] => d
)
M: Array (
[A] =>
[B] => B
)
N: Array (
[B] => c
[D] => d
)
M + n: Array (
[A] =>
[B] => B
[D] => d
)
The result of array_merge (m, n) is: Array (
[A] =>
[B] => c
[D] => d
)

Articles you may be interested in
  • Php searches for the existence of a value in the array (in_array (), array_search (), array_key_exists ())
  • PHP generates continuous numbers (letters) Array Function range () analysis, PHP lottery program function
  • Differences between variables and functions in php after the static keyword is added
  • Use break, continue, goto, return, and exit in multiple loops in PHP
  • Php string replacement function str_replace is faster than preg_replace
  • Php obtains all the files in the directory and saves the results to the array program.
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Performance Comparison of using in_array () foreach array_search () to find whether an array contains

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.