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

How to combine two PHP two-dimensional arrays

小云云
Release: 2023-03-21 06:44:02
Original
43343 people have browsed it


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);
Copy after login

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);
Copy after login

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!

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