Re-understand the php array_merge function
Today, I re-examined the array_merge() function due to a bug.
Definition: array_merge — Merge one or more arrays
Specification: array array_merge(array $array1 [, array $...])
illustrate:
1. Combine the units of one or more arrays, and append the values in one array to the previous array.
2. If the entered array has the same string key name, the value after the key name will override the previous value. However, if the array contains numeric key names, the following values will not overwrite the original value, but will be appended to the following.
3. If only one array is given and the array is numeric indexed, the key names are reindexed in a continuous manner.
This function has detailed examples in the manual, and is also widely used and practical.
One problem I encountered today was that there was a warning in the manual, but I didn't notice it before, which led to a fatal error. as follows:
In PHP >= version 5.0, array_merge() only accepts parameters of array type. However, other types can be merged using casting.
Pay attention to array variables generated by foreach and other codes. Either initialize the variable to an empty array or make a cast during merging. Otherwise, there will be a lot of hardship. Therefore, it is also a good thing to maintain the habit of initializing variables.
In PHP, merge arrays are divided into two situations
1. If there are the same string key names in these two arrays:
<?php $book1 = array('linux'=>'linux server configuration and management','php'=>'PHP Programming'); $book2 = array('linux'=>'Server Configuration and Management','jsp'=>'PHP'); $result = array_merge($book1,$book2); print_r($result); ?>
The output is:
Array ( [linux] => Server configuration and management [php] => PHPProgramming [jsp] => PHP )
Explanation, the latter will replace the former. But if you are using array_merge_recursive(), it can be preserved and exist as a subarray. like:
<?php $book1 = array('linux'=>'linux server configuration and management','php'=>'PHP Programming'); $book2 = array('linux'=>'Server Configuration and Management','jsp'=>'PHP'); $result = array_merge_recursive($book1,$book2); print_r($result); ?>
The output is:
Array ( [linux] => Array ( [0] => linuxServer configuration and management [1] => Server configuration and management ) [php] => PHPProgramming [jsp] => PHP )
2. If there are the same numeric key names in these two arrays:
<?php $book1 = array('linux server configuration and management','PHP Programming'); $book2 = array('Server Configuration and Management','PHP'); $result = array_merge($book1,$book2); print_r($result); ?>
turn out:
Array ( [0] => linuxServer configuration and management [1] => PHPProgramming [2] => Server configuration and management [3] => PHP )
At this time, if the array contains the same numeric key names, the following will not overwrite the previous value, but the following key values will be added in sequence and attached to the following. Do you understand? ^_^