Home  >  Article  >  Backend Development  >  How to merge two arrays into one in php

How to merge two arrays into one in php

WBOY
WBOYOriginal
2023-05-06 09:24:07677browse

In PHP development, arrays are a very important data type, and they often need to be merged or deduplicated. This article will introduce how to use PHP to merge two arrays into one.

Array Merge

PHP provides an array_merge() function to merge two or more arrays and return a new array. The syntax is as follows:

array array_merge ( array $array1 [, array $... ] )

Among them, array1 is a required parameter, indicating the array to be merged , you can pass in multiple arrays, each array separated by commas. Return the merged array.

The following is an example showing how to use the array_merge() function to merge arrays:

$array1 = array("a", "b", "c");
$array2 = array("d", "e", "f");
$result = array_merge($array1, $array2);
print_r($result);

The output results are as follows:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
    [5] => f
)

As you can see, the $array1 and $array2 arrays successfully merged into a new array.

Array deduplication

If you want to deduplicate the merged array, you can use the array_unique() function provided by PHP. The syntax is as follows:

array array_unique (array $array [, int $sort_flags = SORT_STRING ] )

Among them, array is a required parameter, indicating the data to be removed. Array; the sort_flags parameter is optional and represents the sorting rule. The possible values ​​are SORT_STRING (default value, sorted by string) and SORT_NUMERIC (sorted by numerical value).

The following is an example showing how to use the array_unique() function to merge the merged arrays:

$array1 = array("a", "b", "c");
$array2 = array("c", "d", "e");
$result = array_merge($array1, $array2);
$result = array_unique($result);
print_r($result);

The output is as follows:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

In this example, $array1 The $array2 array is merged into a new array $result, and then the array_unique() function is used to remove duplication, and finally the deduplicated array is obtained.

Array merging and deduplication

Combining the above two operations, we can realize the requirement of merging and deduplicating two arrays into one. The following is an example:

$array1 = array("a", "b", "c");
$array2 = array("c", "d", "e", "a");
$result = array_merge($array1, $array2);
$result = array_unique($result);
print_r($result);

The output result is as follows:

Array
(
    [0] => a
    [1] => b
    [2] => c
    [3] => d
    [4] => e
)

In this example, the $array1 and $array2 arrays are merged into a new array $result, and then the array_unique() function is used to Duplicate, and finally get the array after deduplication.

Summary

This article introduces how to use PHP to merge two arrays into one. Among them, the array_merge() function is used for array merging, and the array_unique() function is used for array deduplication. These two functions are very important array operation functions in PHP, and developers need to be proficient in them.

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