Notes for using the php array merge array_merge () function. 1. the example code for merging array_merge () is as follows: $ arrayarray (abb); $ array2array (bcc); $ array3array_merge ($ array, $ array2 ); the output result is Array ([a] bb [B] cc) above 1. array_merge () merge
Example
The code is as follows: |
|
$ Array = array ('a' => 'BB '); $ Array2 = array ('B' => 'CC '); $ Array3 = array_merge ($ array, $ array2 ); The output result is Array ([a] => bb [B] => cc) |
The above is no problem because it is an array. let's set $ array as an array to see what's going on.
The code is as follows: |
|
$ Array = 1; // array ('a' => 'BB '); $ Array2 = array ('B' => 'CC '); $ Array3 = array_merge ($ array, $ array2 ); Print_r ($ array3 ); |
Running result
Warning: array_merge () [function. array-merge]: Argument #1 is not an array in E: test1.php on line 4
It tells us that an array is required, so there are many ways to solve this problem,
1. I used is_array () to determine the data type. However, I found that it is unreasonable to merge multiple arrays. later I found that the data type can be converted.
The code is as follows: |
|
$ Array = 1; // array ('a' => 'BB '); $ Array2 = array ('B' => 'CC '); $ Array3 = array_merge (array) $ array, (array) $ array2 ); Print_r ($ array3 ); No error is reported for the output. Array ([0] => 1 [B] => cc) |
He automatically converts number 1 to an array, so you must pay attention to these details when using it.
Http://www.bkjia.com/PHPjc/816151.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/816151.htmlTechArticle1.array_merge () merge example code as follows $ array = array ('a' = 'BB'); $ array2 = array ('B' = 'CC '); $ array3 = array_merge ($ array, $ array2); the output result is above Array ([a] = bb [B] = cc...