Home  >  Article  >  Backend Development  >  How to combine two PHP two-dimensional arrays

How to combine two PHP two-dimensional arrays

小云云
小云云Original
2018-03-09 16:46:4643274browse


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.

Recommended manual:php complete self-study manual

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.

Recommended related articles:
1.How to delete duplicate elements in a two-dimensional array in PHP? (Pictures + Video)
2.What are the ways to merge two arrays in PHP
Related video recommendations:
1.Dugu Jiujian (4)_PHP video tutorial

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, it becomes array b inserted into array a.

Related recommendations:

Analysis of similarities and differences between three methods of php array merging

About PHP multi-dimensional array merging and sorting functions Detailed explanation

PHP array merging method

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