PHP functions: array_merge and phparray_merge. The PHP function array_merge and phparray_merge reexamined the array_merge () function due to a Bug today. Definition: array_merge merges one or more arrays. Specification: arrayarray_merge PHP function array_merge, phparray_merge
Today, we re-examined the array_merge () function due to a Bug.
Definition: array_merge-merges one or more arrays.
Specification: arrayArray_merge(Array$array1[, Array$...])
Note:
1. Merge the units of one or more arrays. the values in an array are appended to the values of the previous array.
2. if the input array contains the same string key name, the value after the key name overwrites the previous value. However, if the array contains a number key nameNoOverwrite the original value, but append it to the end.
3. if only one array is assigned and the array is digitally indexed, the key name will be re-indexed continuously.
This function provides detailed examples in the manual and is widely used and practical.
One problem encountered today is the warning in the manual, but it was not noticed before, leading to a fatal error. As follows:
PHP> = 5.0,Array_merge () only acceptsArrayType parameter. However, you can use force conversion to merge other types.
Pay attention to the array variables generated by code such as foreach. either initialize the variable as an empty array or perform a forced conversion during merging. Otherwise, you will suffer a lot. Therefore, maintaining the habit of initializing variables is also a good thing.
What is array_merge in php?
Merge arrays
1. if the two arrays have the same string key name:
$ 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 );
?>
Output:
Array ([linux] => server configuration and management [php] => PHP programming [jsp] => PHP)
Note: the latter will replace the former.
2. if the two arrays have the same value key name:
$ Book1 = array (linux server configuration and management, PHP programming );
$ Book2 = array (server configuration and management, PHP );
$ Result = array_merge ($ book1, $ book2 );
Print_r ($ result );
?>
The result is:
Array ([0] => linux server configuration and management [1] => PHP programming [2] => server configuration and management [3] => PHP)
--- Unknown
In php, array_merge cannot add non-array variables. what can be done? In addition to empty
User-defined functions
Function array_add ($ str1, $ str2 ){
If (! Is_array ($ str1 )){
Return;
}
If (is_array ($ str2 )){
$ Result = array_merge ($ str1, $ str2 );
} Else {
$ Result = array_push ($ str1, $ str2 );
}
Return $ result;
}
Parse re-examined the array_merge () function due to a Bug today. Definition: array_merge merges one or more arrays. Specification: array array_merge...