This article describes how PHP uses array_merge to re-arrange the objects under the array, analyzes the array_merge function in detail in the form of an instance, and implements the re-arrangement of array subscripts, for more information, see the following example. Share it with you for your reference. The details are as follows:
An array_unique is used to remove the duplicates in an array. However, it is found that the subscript retains the subscript of the original array. However, php uses the for loop and requires the subscript to be neat, so array_merge can solve this problem by searching for the method to rearrange the objects under the array.
The array_merge () function combines two or more numbers into an array.
If the key name already exists, the key value of the key is the value corresponding to the last key name (the subsequent one overwrites the previous one ). If the array is a digital index, the key name is re-indexed continuously.
Note: If you only input an array to the array_merge () function and the key name is an integer, the function returns a new array with an integer key name, its key name starts from 0 and is re-indexed. (See example 2)
Syntax:
Array_merge (array1, array2, array3 ...)
| Parameters |
Description |
| Array1 |
Required. The first input array. |
| Array2 |
Required. The second input array. |
| Array3 |
Optional. Multiple input arrays that can be specified. |
Example 1
<?php$a1=array("a"=>"Horse","b"=>"Dog");$a2=array("c"=>"Cow","b"=>"Cat");print_r(array_merge($a1,$a2));?>
Output:
Array ([a] => Horse [B] => Cat [c] => Cow)
Example 2
Use only one array parameter:
<?php$a=array(3=>"Horse",4=>"Dog");print_r(array_merge($a));?>
Output:
Array ([0] => Horse [1] => Dog)
I hope this article will help you with php programming.