Home > Backend Development > PHP Tutorial > PHP function introduction—array_merge(): Merge multiple arrays into a new array

PHP function introduction—array_merge(): Merge multiple arrays into a new array

王林
Release: 2023-07-24 16:34:01
Original
2720 people have browsed it

PHP function introduction—array_merge(): Merge multiple arrays into a new array

There are many powerful functions in PHP that can help us process arrays. One of the very useful functions is the array_merge() function. This function can combine multiple arrays into a new array and return this new array. In this article, we will take a closer look at the usage of array_merge() function along with some examples.

The syntax of the array_merge() function is very simple:
array_merge ( array $array1 [, array $... ] ) : array
array_merge() function accepts multiple arrays as parameters and finally returns A new merged array.

Here are some sample codes using the array_merge() function:

Example 1: Merge two arrays

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$result = array_merge($array1, $array2);
print_r($result);
Copy after login

Output results:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
)
Copy after login

In this In the example, we have two arrays $array1 and $array2. By calling the array_merge() function, we merge these two arrays into a new array $result. As you can see, the new array contains all the elements in the original array.

Example 2: Merge multiple arrays

$array1 = array('apple', 'banana', 'orange');
$array2 = array('kiwi', 'melon', 'grape');
$array3 = array('strawberry', 'pineapple');
$result = array_merge($array1, $array2, $array3);
print_r($result);
Copy after login

Output result:

Array
(
    [0] => apple
    [1] => banana
    [2] => orange
    [3] => kiwi
    [4] => melon
    [5] => grape
    [6] => strawberry
    [7] => pineapple
)
Copy after login

In this example, we have three arrays $array1, $array2 and $array3. By calling the array_merge() function, we merge these three arrays into a new array $result. As you can see, the new array contains all the elements in the original array.

Example 3: Merge associative arrays

$array1 = array('name' => 'John', 'age' => 25);
$array2 = array('name' => 'Jane', 'email' => 'jane@example.com');
$result = array_merge($array1, $array2);
print_r($result);
Copy after login

Output results:

Array
(
    [name] => Jane
    [age] => 25
    [email] => jane@example.com
)
Copy after login

In this example, we have two associative arrays $array1 and $array2. Notice that both arrays have the same key, which is 'name'. By calling the array_merge() function, the same keys in the new array $result will be overwritten, that is, the value of the last key-value pair will be retained.

Summary:
The array_merge() function is a very useful function that can help us merge multiple arrays into a new array. It can be used for ordinary arrays and associative arrays, and is concise and efficient. In actual use, we can merge multiple arrays as needed, making data processing more flexible and convenient.

The above is the detailed content of PHP function introduction—array_merge(): Merge multiple arrays into a new array. For more information, please follow other related articles on the PHP Chinese website!

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