Home  >  Article  >  Backend Development  >  php function to merge arrays

php function to merge arrays

(*-*)浩
(*-*)浩Original
2019-09-09 10:06:453694browse

PHP array_merge() function

php function to merge arrays

##Definition and usage

The array_merge() function merges one or more arrays into one array.

Tip: You can input one or more arrays to the function. (Recommended learning:

PHP Programming from Beginner to Master)

Note: If two or more array elements have the same key name, the last element will cover other elements.

Note: If you only input an array to the array_merge() function, and the key names are integers, the function will return a new array with integer key names whose keys start with 0 starts reindexing.

Tip: The difference between this function and the array_merge_recursive() function is that it handles the case where two or more array elements have the same key name. array_merge_recursive() does not perform key name overwriting, but recursively combines multiple values ​​with the same key name into an array.

Syntax

array_merge(array1,array2,array3...)

array1, required. Specifies an array. array2, optional. Specifies an array. array3, optional. Specifies an array.      

Example

Combine two arrays into one array:

<?php
$a1=array("red","green");
$a2=array("blue","yellow");
print_r(array_merge($a1,$a2));
?>

The above is the detailed content of php function to merge arrays. 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