Home  >  Article  >  Backend Development  >  How to merge two arrays in php using array_merge() function

How to merge two arrays in php using array_merge() function

墨辰丷
墨辰丷Original
2018-06-12 14:05:541648browse

This article mainly introduces the method of merging two arrays in PHP through the array_merge() function. It analyzes the usage skills of the array_merge() function in PHP to merge arrays. Friends who need it can refer to this article

The example describes how PHP merges two arrays through the array_merge() function. The specific analysis is as follows:

php merges two arrays through the array_merge() function. array_merge() is a php function that is used to merge two or more arrays. The latter array will be appended to the previous array. , and returns the result array. It accepts two or more arrays and returns an array containing all elements.

$first = array("aa", "bb", "cc");
$second = array(11,22,33);
$third = array_merge($first, $second);
 
foreach ( $third as $val ) {
 print "$val<br />";
}

The execution result of the above code is as follows:

The output is an array with (&#39;aa&#39;, &#39;bb&#39;, &#39;cc&#39;, 11, 22, 33)

Special tip: If the input array has the same string key, the subsequent value of the key will overwrite the previous one. However, if the array contains numeric keys, subsequent values ​​will not overwrite the original values, but will be appended.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php implements syntax highlighting through regular expressions

PHP implements city switching or jump based on IP

php operates the color value and converts the color to its inverse color

The above is the detailed content of How to merge two arrays in php using array_merge() function. For more information, please follow other related articles on the PHP Chinese website!

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