Home > Backend Development > PHP Tutorial > When merging PHP arrays, how to merge different data types?

When merging PHP arrays, how to merge different data types?

王林
Release: 2024-04-28 12:06:01
Original
446 people have browsed it

For merging arrays of different data types in PHP, there are three common methods: 1) array_merge_recursive, recursive merging, retaining sub-arrays; 2) PHP 7.4's spread operator, expanding the array; 3) Custom merge function, providing fine control .

When merging PHP arrays, how to merge different data types?

PHP array merging between different data types

In PHP, when merging arrays, you often encounter different data types Data, in order to achieve the perfect merger of these data types, three common methods are introduced here, and practical cases are provided for demonstration.

1. array_merge_recursive

This function can recursively merge arrays and merge sub-arrays in the form of arrays without losing the data of the sub-arrays.

<?php
$array1 = ['a' => 1, 'b' => [3, 4], 'c' => ['d' => 6]];
$array2 = ['b' => [1, 2], 'e' => 5];

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

print_r($mergedArray); // 输出:
// Array
// (
//     [a] => 1
//     [b] => Array
//         (
//             [0] => 3
//             [1] => 4
//             [2] => 1
//             [3] => 2
//         )
//     [c] => Array
//         (
//             [d] => 6
//         )
//     [e] => 5
// )
?>
Copy after login

2. spread operator (PHP 7.4)

PHP 7.4 introduced the spread operator, which allows spreading an array within another array.

<?php
$array1 = ['a' => 1, 'b' => [3, 4]];
$array2 = ['b' => [1, 2], 'e' => 5];

$mergedArray = [...$array1, ...$array2];

print_r($mergedArray); // 输出:
// Array
// (
//     [0] => 1
//     [1] => 3
//     [2] => 4
//     [3] => 1
//     [4] => 2
//     [5] => 5
// )
?>
Copy after login

3. Custom merge function

If you need more fine-grained control over the merge process, you can write a custom merge function.

<?php
function custom_array_merge(array $array1, array $array2): array
{
    foreach ($array2 as $key => $value) {
        if (isset($array1[$key]) && is_array($array1[$key]) && is_array($value)) {
            $array1[$key] = custom_array_merge($array1[$key], $value);
        } else {
            $array1[$key] = $value;
        }
    }

    return $array1;
}

$array1 = ['a' => 1, 'b' => [3, 4], 'c' => ['d' => 6]];
$array2 = ['b' => [1, 2], 'e' => 5];

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

print_r($mergedArray); // 输出:
// Array
// (
//     [a] => 1
//     [b] => Array
//         (
//             [0] => 3
//             [1] => 4
//             [2] => 1
//             [3] => 2
//         )
//     [c] => Array
//         (
//             [d] => 6
//         )
//     [e] => 5
// )
?>
Copy after login

The above is the detailed content of When merging PHP arrays, how to merge different data types?. 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