Home >Backend Development >PHP Tutorial >PHP uses array_merge to rearrange array subscripts, array_merge array_PHP tutorial

PHP uses array_merge to rearrange array subscripts, array_merge array_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 09:46:09959browse

PHP uses array_merge to rearrange array subscripts, array_merge array

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

<&#63;php
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge($a1,$a2));
&#63;>

Output:

Array ( [a] => Horse [b] => Cat [c] => Cow )

Example 2

Use only one array parameter:

<&#63;php
$a=array(3=>"Horse",4=>"Dog");
print_r(array_merge($a));
&#63;>

Output:

Array ( [0] => Horse [1] => Dog )

I hope this article will be helpful to everyone’s PHP programming design.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1036257.htmlTechArticleHow PHP uses array_merge to rearrange array subscripts, array_merge array This article describes how PHP uses array_merge to rearrange array subscripts method. Share it with everyone for your reference. ...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn