Home  >  Article  >  Backend Development  >  How to use array_merge function in php

How to use array_merge function in php

藏色散人
藏色散人Original
2019-05-24 14:24:013819browse

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.

How to use array_merge function in php

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!

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