PHP combines the values of two numeric key arrays. PHP combines the values of two numeric key arrays. in php, array merging array_merge is the most reliable function, in the following example, when we use the array_merge function to merge, we mainly use PHP to merge the values of two numeric key arrays for the keys of two arrays.
In php, array merge array_merge is the most reliable function. in the following section, when we merge the array_merge function, we mainly process the key values of the two arrays as numbers.
First, you must understand a basic knowledge point.
In PHP, you can use + or array_merge to merge two arrays, but there are still differences between them. if you do not know the differences, the project will be terrible!
The main difference is that if the same key name appears in two or more arrays, the key name can be divided into strings or numbers.
1) when the key name is a number, the value after array_merge () will not overwrite the original value, but will be appended to the back, however, the + merge array will return the first value as the final result, and discard the values with the same key name in the subsequent array (not overwrite)
2) when the key name is a string, array_merge () overwrites the value with the same key name, but + returns the first value as the final result, the values of the following arrays with the same key names are discarded (not overwritten ).
| The code is as follows: |
|
|
$ A = array ('A', 'B ');
$ B = array ('C', 'D ');
$ C = $ a + $ B;
Var_dump ($ );
Var_dump (array_merge ($ a, $ B ));
// Output:
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'D' (length = 1)
++ ++
$ A = array (
0 => 'A ',
1 => 'B'
);
$ B = array (
0 => 'C ',
1 => 'B'
);
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));
// Output:
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1)
++ ++
$ A = array ('A', 'B ');
$ B = array (
'0' => 'C ',
1 => 'B'
);
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));
// Output:
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1)
++ ++
$ A = array (
0 => 'A ',
1 => 'B'
);
$ B = array (
'0' => 'C ',
'1' => 'B'
);
$ C = $ a + $ B;
Var_dump ($ c );
Var_dump (array_merge ($ a, $ B ));
Output:
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
Array
0 => string 'A' (length = 1)
1 => string 'B' (length = 1)
2 => string 'C' (length = 1)
3 => string 'B' (length = 1) |
Let's take a look at the value of merging two numeric key arrays.
| The code is as follows: |
|
|
/**
* PHP combines the values of two numeric key arrays
*
* @ Param array $ arr1
* @ Param array $ arr2
* @ Return array
* @ Author www.111cn.net
*/
Function new_array_merge ($ arr1, $ arr2)
{
$ Arr = array_flip ($ arr1) + array_flip ($ arr2 );
Return array_keys ($ arr );
}
$ Arr1 = array ('A', 'BB ', 'CC ');
$ Arr2 = array ('aa2 ', 'BB', 'cc2 ');
$ Arr = new_array_merge ($ arr1, $ arr2 );
Print_r ($ arr );
Output:
Array
(
[0] => aa
[1] => bb
[2] => cc
[3] => aa2
[4] => cc2
) |
Array merge array_merge in merge php is the most reliable function. we will use the array_merge function to merge the keys of the two arrays below...