PHP array function array_merge

墨辰丷
Release: 2023-03-27 11:22:02
Original
1514 people have browsed it

This article mainly introduces the PHP array function array_merge. Interested friends can refer to it. I hope it will be helpful to everyone.

I have used this function a long time ago, but it was just a simple use and I did not do too in-depth research.

Today I saw some uses of array_merge while browsing other people's blogs. Experience, so I will make a summary myself.

array_merge is to merge one or more arrays.

This function is mostly used for merging result sets retrieved from the database.

The parameter configuration is also very simple array_merge(arr1, arr2, arrN)

Note that the parameters here must be arrays, otherwise an error will be reported.

Although it seems very simple, there are also many pitfalls.

We will analyze from the direction of single array and multiple array.

1. Merge multiple arrays (custom key name):

From the following running results It can be seen that the data with the same key name in $arr1 and $arr2 has been overwritten.

The following array will overwrite the values ​​with the same keys in the previous array.

<?php

$arr1 = array(&#39;a&#39;=>&#39;1&#39;,&#39;b&#39;=>&#39;2&#39;);
$arr2 = array(&#39;a&#39;=>&#39;1&#39;,&#39;b&#39;=>&#39;3&#39;,&#39;c&#39;=>&#39;2&#39;);

$ret = array_merge($arr1,$arr2);
print_r($ret);



//run result
Array
(
  [a] => 1
  [b] => 3
  [c] => 2
)
Copy after login

2. Merge multiple arrays (customized numeric key name):

If the key name in the array is numeric, the key name will be formatted and all key values ​​will be retained.

<?php

$arr1 = array(1=>&#39;1&#39;,2=>&#39;2&#39;);
$arr2 = array(1=>&#39;1&#39;,2=>&#39;3&#39;,6=>&#39;2&#39;);

$ret = array_merge($arr1,$arr2);
print_r($ret);




//run result

Array
(
  [0] => 1
  [1] => 2
  [2] => 1
  [3] => 3
  [4] => 2
)
Copy after login

3. Merge multiple arrays (no key name input):

<?php

$arr1 = array(1,2);
$arr2 = array(1,2,6);

$ret = array_merge($arr1,$arr2);
print_r($ret);



//run result
Array
(
  [0] => 1
  [1] => 2
  [2] => 1
  [3] => 2
  [4] => 6
)
Copy after login

4. Most of the merge operations (when an array is empty):

An array Empty operation is very common, and the existing value will be displayed no matter which array is empty.

<?php

$arr1 = array();
$arr2 = array(1,2,6);

$ret = array_merge($arr1,$arr2);
print_r($ret);


//run result
Array
(
  [0] => 1
  [1] => 2
  [2] => 6
)
Copy after login

5. Single array operation:

This situation of single array is generally not allowed Array_merge() will be used because this function is used to merge arrays.

But if you want to restore the original related key name, you can use this function, but it is not recommended.

Because there is a better function than it, you can use array_values()

The single array operation is actually very similar to the two cases 2 and 3 above. When the key name is a number, the format will be Change the key name, otherwise it will be displayed directly.

<?php

$arr1 = array(1=>1,3=>2,6=>6);

$ret = array_merge($arr1);
print_r($ret);


//run result
Array
(
  [0] => 1
  [1] => 2
  [2] => 6
)
Copy after login

6. When two arrays are merged, you can use array_merge(). The latter array will be merged with the previous array. Merge

But what should I do if I want to merge the previous array with the later array?

Should we change the positions of the two arrays in array_merge()? There is actually an easier way.

Just use " " to complete the operation.

<?php
$arr1 = array(&#39;a&#39;=>1,&#39;b&#39;=>2);
$arr2 = array(&#39;a&#39;=>1,&#39;b&#39;=>3,&#39;c&#39;=>6);

$ret = $arr1+$arr2;
print_r($ret);



//run result
Array
(
  [a] => 1
  [b] => 2
  [c] => 6
)
Copy after login

Related recommendations:

imagecopymerge() function detailed steps to create a translucent watermark

array_mergeFunction introduction and detailed explanation of usage

Emergence Detailed explanation on the use of .js plug-in

The above is the detailed content of PHP array function array_merge. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!