Home > Backend Development > PHP Tutorial > How to use array_merge function in php

How to use array_merge function in php

藏色散人
Release: 2023-04-06 19:42:01
Original
3869 people have browsed it

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...)
Copy after login

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));
?>
Copy after login

Output:

Array ( [0] => 西门 [1] => 灭绝 [2] => 无忌 [3] => peter )
Copy after login

php array_merge() function usage example 2

<?php
$str1=array("a"=>"西门","b"=>"灭绝");
$str2=array("c"=>"欧阳克","b"=>"无忌");
print_r(array_merge($str1,$str2));
?>
Copy after login

Output:

Array ( [a] => 西门 [b] => 无忌 [c] => 欧阳克 )
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template