For example, we encountered in the project need to merge two arrays or multiple arrays into an array of cases, in fact, as long as the knowledge of the relevant functions, the future of such problems can be handy. Here are some examples of how PHP arrays are merged with specific code samples.
Note: There are three types of arrays, indexed arrays; associative arrays; multidimensional arrays.
An example of an array merge code is as follows:
First, PHP merge two ordinary arrays
<?phpheader ("Content-type:text/html;charset=utf-8"); Set the encoding? ><?php$ar1=array (' iphone ', ' Samsung ', ' Xiaomi '), $ar 2=array (' Huawei ', ' Glory ', ' Nokia ');p Rint_r (Array_merge ($ar 1, $ar 2));? >
There are two ordinary arrays in the code above, and we combine the two numbers with the Array_merge function and print out the results such as:
It is clear that 2 arrays have been successfully merged into an array. I believe we all notice the array_merge function , yes, thearray_merge function is the ability to merge multiple arrays.
Two, PHP merge two associative arrays
<?php$ar1=array (' a ' + = ' iphone ', ' B ' + = ' Samsung ', ' C ' + ' Xiaomi '); $ar 2=array (' a ' + = ' Huawei ', ' d ' + ' ' glory ', ' e ' = = ') Nokia ');p Rint_r (Array_merge ($ar 1, $ar 2));? >
Merges two associative arrays and accesses the results through a browser such as:
At this time, do not know whether people find a problem? The two associative arrays have an identical key name, and after merging, the next key name value overrides the additional value. That is, if you have the same key name in multiple arrays, then multiple array merges with the Array_merge function will have one element covering the other.
At the end of the note, we also show you another use of the array_merge merge function, that is, if we put an array with a key name as an integer and enter it into this function, we will get a new array that is re-indexed!
So the above is about PHP merging 2 arrays and multiple array of data specific methods introduced, which mainly require you to understand the Array_merge function. This article has certain reference value, hoped to have the need friend to have the help!