- Array ([0] = Horse [1] = Dog)
- Array ([3] = Horse [4] = Dog)
Answer a
Instance
Combine two arrays into an array:
<?php$a1=array ("Red", "green"), $a 2=array ("Blue", "yellow");p Rint_r ( array_merge($a1,$a2)
);? >
Running an instance
Definition and usage
The Array_merge () function merges one or more arrays into an array.
Tip: You can enter one or more arrays into the function.
Note: If two or more array elements have the same key name, the last element overrides the other element.
Note: If you enter an array only to the Array_merge () function, and the key name is an integer, the function returns a new array with an integer key name, with the key name re-indexed starting at 0 (see Example 1 below).
Tip: The difference between this function and the array_merge_recursive () function is that it handles two or more arrays of elements with the same key name. Array_merge_recursive () does not overwrite the key name, but instead recursively composes the values of multiple identical key names into an array.
Grammar
Array_merge (array1,array2,array3...)
Parameters |
Description |
Array1 |
Necessary. Specifies the array. |
Array2 |
Optional. Specifies the array. |
Array3 |
Optional. Specifies the array. |
Technical details
return value: |
Returns the merged array. |
PHP version: |
4 + |
Update log: |
Since PHP 5.0, this function only accepts parameters of the array type. |
More examples of examples 1
Merge two associative arrays into an array:
<?php$a1=array ("A" and "Red", "B" and "green"), $a 2=array ("c" = "Blue", "b" = "yellow");p Rint_r ( array_merge($a1,$a2)
); >
Running an instance
Example 2
Use only one array parameter with an integer key name:
<?php$a=array (3=> "Red",4=> "green");p Rint_r ( array_merge($a)
);? >
Running an instance
[Single topic]<?php$a=array (3=> "Horse",4=> "Dog");p Rint_r (Array_merge ($a));? >