Re-recognize PHP array_merge function, Phparraymerge
Re-recognize PHP array_merge functions
Today, a bug re-examines the next Array_merge () function.
Definition: array_merge-merging one or more arrays
Spec: Array array_merge (array $array 1 [, Array $ ...])
Description
1. Merge the cells of one or more arrays, and the values in one array are appended to the previous array.
2. If the input array has the same string key name, the value following the key name overrides the previous value. However, if the array contains numeric key names, subsequent values will not overwrite the original values, but are appended to the back.
3. If only one array is given and the array is a numeric index, the key name is re-indexed in a sequential manner.
This function has a detailed example in the manual and is very versatile and useful.
One of the problems encountered today is the warning in the manual, which was not noticed before, which led to a fatal error. As follows:
PHP >= version 5.0, Array_merge () only accepts parameters of type array. However, you can use casts to merge other types.
Be aware of array variables that are generated by code such as foreach, either initialize the variable to an empty array, or make a cast at merge time. Otherwise, there will be a great hardship to eat. Therefore, it is a good thing to keep a habit of initializing variables.
Merging arrays in PHP is divided into two cases
1, if the two arrays have the same string key name:
<?php $book 1 = array (' Linux ' + ' Linux server configuration and Management ', ' php ' + ' PHP programming '); $book 2 = Array (' Linux ' = ' Server configuration and Management ' , ' jsp ' = ' PHP '); $result = Array_merge ($book 1, $book 2); Print_r ($result);? >
The output is:
Array ([Linux] = server configuration and management [PHP] + PHP programming [JSP] + PHP)
The latter will replace the former. However, if you are using array_merge_recursive (), you can persist and make a subarray exist. Such as:
<?php $book 1 = array (' Linux ' + ' Linux server configuration and Management ', ' php ' + ' PHP programming '); $book 2 = Array (' Linux ' = ' Server configuration and Management ' , ' jsp ' = ' PHP '); $result = array_merge_recursive ($book 1, $book 2); Print_r ($result);? >
The output is:
Array ([Linux] = = Array ([0] = + Linux server configuration and management [1] = + server configuration and Management) [PHP] + PHP programming [JSP] + PHP)
2, if the two arrays have the same numeric key name:
<?php $book 1 = array (' Linux server Configuration and Management ', ' PHP programming '), $book 2 = array (' Server configuration and Management ', ' PHP '); $result = Array_merge ($book 1, $book 2); Print_r ($result);? >
The result is:
Array ([0] = + Linux server configuration and management [1] + PHP programming [2] + = Server configuration and management [3] = PHP)
At this point, if the array contains the same numeric key name, then the previous value is not overwritten, but the subsequent key values are incremented sequentially, appended to the back. Do you understand, ^_^
There are no functions (or tips) for merging array values in PHP
1. The simplest way: Use +
The following code:
$r 1 = Array (' A ' =>1, ' B ' =>3, ' C ' =>5, ' d ' =>7, ' e ' =>9);
$r 2 = Array (' F ' =>2, ' G ' =>4, ' h ' =>6, ' I ' =>8, ' J ' =>10);
$r = $r 1+ $r 2;
Echo '
';
Print_r ($R);
Echo ' </pre> ';
?>
Code Output results:
Array
(
[A] = 1
[B] = 3
[C] = 5
[D] = 7
[E] = 9
[F] = 2
[g] = 4
[H] = 6
[I] = 8
[j] = 10
)
But this approach has a need to pay attention to the place: two keywords the same element will be retained only one, that is, $R1+R2, the data in the $R1 is preserved, this can be used on special occasions may also happen.
2. Use the Array_merge () function:
$array 1 = Array ("Color" = "Red", 2, 4);
$array 2 = Array ("A", "B", "color" = "green", "shape" = "trapezoid", 4);
$result = Array_merge ($array 1, $array 2);
Print_r ($result);
?>
Code Output results:
Array
(
[Color] = Green
[0] = 2
[1] = 4
[2] = a
[3] = b
[Shape] = trapezoid
[4] = 4
)
What is & in PHP's function: Private function Test (& $array) {}
This is the invocation of the function's address. $array is a variable, passed to test () is the address of the $array, the function directly overwrites the $array content return value
http://www.bkjia.com/PHPjc/872374.html www.bkjia.com true http://www.bkjia.com/PHPjc/872374.html techarticle re-understand the PHP array_merge function, Phparraymerge re-understand PHP array_merge function today because of a bug re-examined the next array_merge () this function. Definition: array_merge-Merge One ...