Home > Backend Development > PHP Problem > There are several ways to merge arrays in php

There are several ways to merge arrays in php

青灯夜游
Release: 2023-03-10 08:48:01
Original
27101 people have browsed it

There are 4 ways to merge arrays in php, which are: 1. Use the plus sign " " operator, the syntax is "array 1 array 2 .."; 2. Use the array_push() function; 3. Use array_merge( ) function; 4. Use the array_merge_recursive() function.

There are several ways to merge arrays in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

1. Use the plus sign " " operator:

Directly merge the values ​​​​of the two arrays into one array;

$a = [1,2,3,'a'=>'a'];
$b = ['a'=>'b',4,5,'b'=>'c',6,7,'a'];
$c = $a + $b;

print_r($a);
print_r($b);
print_r($c);

//$a
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [a] => a
)

//$b
Array
(
    [a] => b
    [0] => 4
    [1] => 5
    [b] => c
    [2] => 6
    [3] => 7
    [4] => a
)

//$c
Array
(
    [0] => 1
    [1] => 2
    [2] => 3
    [a] => a
    [b] => c
    [3] => 7
    [4] => a
)
Copy after login

Use " " to merge arrays. If the keys are the same, the previous array value will overwrite the later array value.

2. Use the array_push() function

Put a variable into another array

$a = [1,'a'=>'aa',2,'b'=>'bb'];
$b = [1,'a'=>'aa',2,'b'=>'bb'];
print_r($a);
print_r($b);
array_push($a, $b);
print_r($a);

//$a
Array
(
    [0] => 1
    [a] => aa
    [1] => 2
    [b] => bb
)

//$b
Array
(
    [0] => 1
    [a] => aa
    [1] => 2
    [b] => bb
)

// array_push过后的$a
Array
(
    [0] => 1
    [a] => aa
    [1] => 2
    [b] => bb
    [2] => Array
        (
            [0] => 1
            [a] => aa
            [1] => 2
            [b] => bb
        )

)
Copy after login

It can be seen from the printed value that after the variable enters the array, The corresponding numeric index must be the smallest numeric index 1 in the current array (if the current array does not have a numeric index, the index corresponding to the new element is 0)

3. Use the array_merge() function

Directly merge two arrays. If the keys of the two arrays are the same, the value of the latter array will overwrite the value of the previous array.

$a = [1,'2'=>2,'a'=>'a','b'=>'b'];
$b = [1,'2'=>3,'a'=>'c','b'=>'d','c'=>'e'];
$c = array_merge($a,$b);
print_r($a);
print_r($b);
print_r($c);

//$a
Array
(
    [0] => 1
    [2] => 2
    [a] => a
    [b] => b
)

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

//$c
Array
(
    [0] => 1
    [1] => 2
    [a] => c
    [b] => d
    [2] => 1
    [3] => 3
    [c] => e
)
Copy after login

It can be seen that if the keys of the two arrays are the same, the value of the latter array will overwrite the value of the previous array. The value of the previous array. However, for numeric indexes or numeric string indexes, they will be reset in order (the first numeric index element of the first array is filled sequentially starting from 0)

4. Use the array_merge_recursive() function

Merge arrays, when encountering the same key, merge the values ​​in the key into a sub-array

$a = [1,'2'=>2,'a'=>'a','b'=>'b'];
$b = [1,'2'=>3,'a'=>'a','b'=>'d','c'=>'e'];
$c = array_merge_recursive($a,$b);
print_r($a);
print_r($b);
print_r($c);


//$a
Array
(
    [0] => 1
    [2] => 2
    [a] => a
    [b] => b
)

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

//$c
Array
(
    [0] => 1
    [1] => 2
    [a] => Array
        (
            [0] => a
            [1] => a
        )

    [b] => Array
        (
            [0] => b
            [1] => d
        )

    [2] => 1
    [3] => 3
    [c] => e
)
Copy after login

When using the "array_merge_recursive" method, the elements with the same key will be integrated into a new array, keeping The key name remains unchanged and is used as a sub-element of the merged array (the array combined by $a and $b). But for numeric indexes or numeric string indexes, they will be reset in order (the first numeric index element of the first array is filled sequentially starting from 0). Even if the corresponding numerical indexes are the same, elements with the same index will not be integrated into a subarray.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of There are several ways to merge 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