Home  >  Article  >  Backend Development  >  PHP merge array + the difference between array_merge_PHP tutorial

PHP merge array + the difference between array_merge_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:31:21909browse

In PHP, you can use + or array_merge to merge two arrays, but there are still differences between the two. A clear understanding of the difference between the two processing methods is still very necessary for the rapid development of the project.

The main difference is that when the same key name appears in two or more arrays, you need to pay attention to the following two points:

First of all, it is necessary to explain that array key names in PHP can be roughly divided into strings (associative arrays) or numbers (numeric arrays). Multi-dimensional arrays will not be discussed here.

(1) When the key name is a number (numeric array), array_merge() will not overwrite the original value, but + merge array will return the first value as the final result, and the subsequent arrays with the same key name will be returned Those values ​​are "discarded" (not overwritten).

(2) When the key name is a character (associative array), + still returns the first appearing value as the final result, and "discards" those values ​​in subsequent arrays with the same key name, but array_merge() will overwrite it at this time Remove the previous value with the same key name.

The following is explained through several specific examples:

m:Array (
[0] => a
[1] => b
)
n:Array (
[0] => c
[1] => d
)
The result of m+n is: Array (
[0] => a
[1] => b
)
The result of array_merge(m,n) is: Array (
[0] => a
[1] => b
[2] => c
[3] => d
)
m:Array (
[1] => a
[2] => b
)
n:Array (
[2] => c
[3] => d
)
The result of m+n is: Array (
[1] => a
[2] => b
[3] => d
)
The result of array_merge(m,n) is: Array (
[0] => a
[1] => b
[2] => c
[3] => d
)
m:Array (
[a] => a
[b] => b
)
n:Array (
[b] => c
[d] => d
)
The result of m+n is: Array (
[a] => a
[b] => b
[d] => d
)
The result of array_merge(m,n) is: Array (
[a] => a
[b] => c
[d] => d
)

Articles you may be interested in

  • php finds whether a value exists in an array (in_array(), array_search(), array_key_exists())
  • PHP generates continuous numeric (letter) array function range() analysis, PHP lottery program function
  • Differences in PHP after adding the static keyword before variables and functions
  • Multiple popup in PHP The usage and difference of using break, continue, goto, return, and exit in a loop
  • php string replacement function str_replace is faster than preg_replace
  • php program to get all files in a directory and save the results to an array
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption to optimize the performance of the program
  • About the performance comparison when using in_array() foreach array_search() to find whether the array is contained

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/764102.htmlTechArticleIn PHP, you can use + or array_merge to merge two arrays, but there is still a difference between the two. Please understand clearly The difference between these two processing methods is still very necessary for the rapid development of the project...
Statement:
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