Home  >  Article  >  Backend Development  >  Code implementation of vertical merging & horizontal merging of two-dimensional arrays in PHP

Code implementation of vertical merging & horizontal merging of two-dimensional arrays in PHP

不言
不言Original
2018-08-20 17:06:222403browse

The content of this article is about the code implementation of vertical merging & horizontal merging of two-dimensional arrays in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

This article mainly shares with you the first merging method: merging an array through the array_merge method given by PHP's array API. I hope it can help everyone.

Code:

 $a = array(array("1","2"),array("3","4"));
$b = array(array("a","b"),array("c","d"));          
$c = array_merge($a,$b);
print_r($c);

Result:

After the combination of these methods, the b array is appended to the a array.

Second merging method:

Merge arrays through foreach loop

Code:

<?php
$a = array(array("1","2"),array("3","4"));
$b = array(array("a","b"),array("c","d"));  
foreach($a as $key=>$vo)
{
$list[] = array_merge($vo,$b[$key]);
} 
print_r($list);

Result:

After merging, this method inserts array b into array a.

Related recommendations:

Solution to float to int distortion in PHP

How to use cURL to call WebService to obtain weather information in PHP ( Code)

The above is the detailed content of Code implementation of vertical merging & horizontal merging of two-dimensional 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