Home > Article > Backend Development > How to use array_merge function in php
Usage of array_merge function in php: [array_merge(array)]. The array_merge function is used to merge one or more arrays into one array. If multiple array elements have the same key name, the last array element overwrites the other array elements.
php The array_merge function is used to merge one or more arrays into one array. Its syntax is array_merge(array1, array2, array3...), parameters array1 is required and specifies the array.
(Recommended tutorial: php video tutorial)
How to use the php array_merge function?
Function: Combine one or more arrays into one array.
Syntax:
array_merge(array1,array2,array3...)
Parameters:
array1 Required. Specifies an array.
array2 Optional. Specifies an array.
array3 Optional. Specifies an array.
Instructions:
You can input one or more arrays to the function. If two or more array elements have the same key name, the last element overwrites the others. If you input only an array to the array_merge() function, and the keys are integers, the function will return a new array with integer keys, re-indexed starting at 0.
php array_merge() function usage example 1
<?php $name1=array("西门","灭绝"); $name2=array("无忌","peter"); print_r(array_merge($name1,$name2)); ?>
Output:
Array ( [0] => 西门 [1] => 灭绝 [2] => 无忌 [3] => peter )
php array_merge() function usage example 2
<?php $str1=array("a"=>"西门","b"=>"灭绝"); $str2=array("c"=>"欧阳克","b"=>"无忌"); print_r(array_merge($str1,$str2)); ?>
Output:
Array ( [a] => 西门 [b] => 无忌 [c] => 欧阳克 )
The above is the detailed content of How to use array_merge function in php. For more information, please follow other related articles on the PHP Chinese website!