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

How to merge arrays in PHP

藏色散人
藏色散人 Original
2018-11-23 16:09:35 6447browse

This article mainly introduces howPHP 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:


      

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 functionmeans 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 )

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!

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