Home > Backend Development > PHP Problem > How to combine two arrays in php

How to combine two arrays in php

PHPz
Release: 2023-04-18 09:41:59
Original
768 people have browsed it

In PHP, we can use the array_merge() function to merge two arrays into a new array. This function accepts any number of arrays as arguments, passed in any order. The syntax for using this function is as follows:

$new_array = array_merge($array1, $array2);
Copy after login

where $array1 and $array2 are the two arrays to be combined.

It should be noted that if the key values ​​​​of the two arrays are the same, the value of the latter array will overwrite the value of the previous array. If the array has values ​​of different types, the values ​​will retain their current type in the new array. If you want to ensure that the resulting new array has consecutive numerical indices, you can use another function array_merge_recursive().

$new_array = array_merge_recursive($array1, $array2);
Copy after login

This function is similar to array_merge(), but it can handle duplicate key values ​​and generate nested arrays within new arrays.

Here are some sample codes:

$array1 = array('a', 'b', 'c');
$array2 = array('d', 'e', 'f');
$result = array_merge($array1, $array2);
print_r($result); // 输出 Array ( [0] => a [1] => b [2] => c [3] => d [4] => e [5] => f )

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'b' => 5, 'f' => 6);
$result = array_merge($array1, $array2);
print_r($result); // 输出 Array ( [a] => 1 [b] => 5 [c] => 3 [d] => 4 [f] => 6 )

$array1 = array('a' => 1, 'b' => 2, 'c' => 3);
$array2 = array('d' => 4, 'b' => 5, 'f' => 6);
$result = array_merge_recursive($array1, $array2);
print_r($result); // 输出 Array ( [a] => 1 [b] => Array ( [0] => 2 [1] => 5 ) [c] => 3 [d] => 4 [f] => 6 )
Copy after login

In some cases, you may need to combine two arrays according to certain rules. To achieve this, you can use the array_combine() function. This function takes values ​​from one array as keys, values ​​from another array as values, and returns a new array. This function requires two parameters, the key array and the value array to be converted.

$keys = array('a', 'b', 'c');
$values = array(1, 2, 3);
$result = array_combine($keys, $values);
print_r($result); // 输出 Array ( [a] => 1 [b] => 2 [c] => 3 )
Copy after login

In practical applications, combining arrays is a very useful technique. You can combine two arrays into one for manipulation and separate them when needed. In addition, combined arrays can also be used in parsing CSV files, creating form fields, etc.

The above is the detailed content of How to combine two arrays in php. 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