Sun May 31 2020

Array Merge

PHP Scripting860 views

File Name: array-merge.php

<?php
	$array1 = array(1,2,3);
	$array2 = array(10,20,30);
	
	/* Merge two array */
	$merge1 = array_merge($array1,$array2);
	
	$array3 = array(1 => 'PHP', 2 => 'Java', 3 => 'C++');
	$array4 = array(10 => 'C', 11 => 'Python', 12 => 'JSP');
	
	/* Merge two associative array */
	$merge2 = array_merge($array3, $array4);	
	
	var_dump($merge1);
	echo '<br />';
	var_dump($merge2);
	
	/* Merge associative array with '+' operator */
	echo '<br />'.var_export($array3 + $array4, TRUE);
?>



/* Output */
array(6) { [0]=> int(1) [1]=> int(2) [2]=> int(3) [3]=> int(10) [4]=> int(20) [5]=> int(30) }
array(6) { [0]=> string(3) "PHP" [1]=> string(4) "Java" [2]=> string(3) "C++" [3]=> string(1) "C" [4]=> string(6) "Python" [5]=> string(3) "JSP" }
array ( 1 => 'PHP', 2 => 'Java', 3 => 'C++', 10 => 'C', 11 => 'Python', 12 => 'JSP', )

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.