Home > Backend Development > PHP Tutorial > How to merge two arrays in PHP

How to merge two arrays in PHP

王林
Release: 2023-07-07 09:58:01
Original
2384 people have browsed it

How to merge two arrays in PHP

In PHP programming, you often encounter situations where you need to merge two arrays. PHP provides a variety of methods to implement array merging operations. This article will introduce several of the common methods, with code examples.

Method 1: Use array_merge function

The array_merge function is a built-in function provided by PHP for merging arrays. It accepts multiple arrays as parameters and returns a merged new array.

The following is a code example of using the array_merge function to merge two arrays:

$array1 = array('apple', 'banana', 'pear');
$array2 = array('orange', 'grape', 'kiwi');

$mergedArray = array_merge($array1, $array2);

print_r($mergedArray);
Copy after login

The output result is:

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

Method 2: Use the addition operator ( )

In PHP, you can use the addition operator () to merge two arrays. This operator merges the elements of two arrays into a new array.

The following is a code example that uses the addition operator to merge two arrays:

$array1 = array('apple', 'banana', 'pear');
$array2 = array('orange', 'grape', 'kiwi');

$mergedArray = $array1 + $array2;

print_r($mergedArray);
Copy after login

The output result is:

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

It should be noted that if the same exists in the two arrays key names, the addition operator will retain the values ​​in the first array and ignore the values ​​in the second array.

Method 3: Use the array_merge_recursive function

The array_merge_recursive function is a function that recursively merges arrays. It can recursively merge two or more arrays and return a new array. Unlike the array_merge function, the array_merge_recursive function does not simply merge elements with the same key name directly, but converts them into a subarray containing the same key name.

The following is a code example of using the array_merge_recursive function to merge two arrays:

$array1 = array('apple', 'banana', 'pear');
$array2 = array('orange', 'grape', 'kiwi');

$mergedArray = array_merge_recursive($array1, $array2);

print_r($mergedArray);
Copy after login

The output result is:

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

As mentioned above, the array_merge_recursive function will merge elements with the same key name Convert to a subarray, which may be what we want in some cases.

Summary:

This article introduces three commonly used array merging methods in PHP: using the array_merge function, using the addition operator (), and using the array_merge_recursive function. According to different needs, choosing the appropriate method can more conveniently implement array merging operations.

The above is the detailed content of How to merge two arrays 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