Home > Daily Programming > PHP Knowledge > How to merge arrays in PHP

How to merge arrays in PHP

藏色散人
Release: 2021-01-11 10:43:38
Original
6560 people have browsed it

This article mainly introduces how PHP implements the merger of two arrays, and the value of one array is the subscript, and the value of the other array is the corresponding value.

PHP merges ordinary arrays, I believe everyone has mastered it. But for novice friends, when merging two arrays, the subscript and value of the new array are the values ​​of the two arrays before merging respectively. The processing of this kind of array merging may be a little difficult. In fact, it is also very easy to implement.

Below we will explain it to you with simple code examples.

The code is as follows:

<?php
$arr = [&#39;姓名&#39;,&#39;年龄&#39;,&#39;性别&#39; ];
$arr2 = [&#39;王小二&#39;,24,&#39;男&#39;];

var_dump(array_combine($arr,$arr2));
Copy after login

The result is as shown below:

How to merge arrays in PHP

##As shown in the figure, here is a new array, first The values ​​'name', 'age', 'gender' of the first array $arr become the subscripts of the new array; the values ​​'Wang Xiaoer', 24, 'male' of the second array $arr2 become the subscripts of the new array. The values ​​of the new array.


In our actual project development process, this method of merging arrays is also very common.

Then the important function involved is array_combine.


array_combine function means creating an array, using the value of one array as its key name and the value of another array as its value.

array_combine syntax is as follows:

array array_combine ( array $keys , array $values )
Copy after login
Returns an array, in which the parameters are represented respectively, using the value from the keys array as the key name, and the value from the values ​​array as the corresponding value.

This article is a detailed introduction to PHP merging arrays. It is very easy to understand. I hope it will be helpful to friends in need!

The above is the detailed content of How to merge arrays in PHP. 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