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

How to combine two arrays into one array in php

PHPz
PHPzOriginal
2023-04-19 11:39:041767browse

In PHP, sometimes you need to merge two arrays into one array to process data more conveniently. PHP provides a variety of methods to achieve this purpose, including using the array_merge() function and the plus operator. ( ) and array_combine() function, etc.

1. Use the array_merge() function

The array_merge() function is one of PHP’s built-in functions, used to merge two or more arrays into one array. It appends the elements from the second array to the end of the first array, resulting in a new array. The following is a sample code that uses the array_merge() function to merge two arrays:

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

In the above code, $array1 and $array2 represent two arrays respectively, $result is a new array, and the array_merge() function will $ The elements in array2 are appended to the end of $array1, forming a new array containing all elements.

2. Use the plus sign operator ( )

The plus sign operator ( ) can also be used to combine two arrays. The plus sign adds the first array to the second array. If they have the same key name, the value of the second array will overwrite the value of the first array. The following is a sample code that uses the plus sign operator to combine two arrays:

$array1 = array("a" => "apple", "b" => "banana", "c" => "carrot");
$array2 = array("d" => "dog", "e" => "elephant", "f" => "fox");
$result = $array1 + $array2;
print_r($result);

In the above code, $array1 and $array2 represent two arrays respectively, $result is a new array, use the plus sign operator to combine The elements in $array2 are added to the end of $array1, ensuring the uniqueness of the key names, and finally forming a new array containing all elements.

3. Use the array_combine() function

The array_combine() function is another function built into PHP, which is used to combine two arrays into one array. But unlike the array_merge() function and the plus operator, it can only combine two arrays, and the first array must contain key names, and the second array must contain values. The following is a sample code that uses the array_combine() function to combine two arrays:

$keys = array("a", "b", "c");
$values = array("apple", "banana", "carrot");
$result = array_combine($keys, $values);
print_r($result);

In the above code, $keys and $values ​​represent two arrays respectively, $result is a new array, use the array_combine() function to combine The elements in $keys are used as key names, and the elements in $values ​​are used as values, ultimately forming a new array containing all elements.

In short, PHP provides a variety of methods to combine two arrays, among which the array_merge() function, the plus operator ( ) and the array_combine() function are common and commonly used methods. Which method to choose depends on the actual needs. Different methods have different advantages and disadvantages, and the choice needs to be based on the specific situation.

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