This article describes the example of PHP using array_merge to rearrange array subscripts. Share it with everyone for your reference. The details are as follows:
I used an array_unique to remove duplication in an array, but found that the subscript retained the subscript of the original array, but PHP needs to use a for loop to neatly subscript, so looking for a way to rearrange the array subscripts array_merge can solve this problem
array_merge() function merges two or more arrays into one array.
If there are duplicate key names, the key value of the key will be the value corresponding to the last key name (the later one will overwrite the previous one). If the array is numerically indexed, the key names are re-indexed consecutively.
Note: If only an array is input to the array_merge() function, and the keys are integers, the function will return a new array with integer keys, with keys re-indexed starting from 0. (see example 2)
Grammar:
array_merge(array1,array2,array3...)
参数 | 描述 |
---|---|
array1 | 必需。输入的第一个数组。 |
array2 | 必需。输入的第二个数组。 |
array3 | 可选。可指定的多个输入数组。 |
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 be helpful to everyone’s PHP programming design.