Jump to: navigation, search

Php array merge

From w3cyberlearnings

Contents

PHP function array_merge

This function merges two or more arrays into a new array. Duplicate array will be overrided.

Syntax array_merge

  • array1: array 1 input
  • array2: array 2 input
  • array3: array 3 input
array_merge(array1, array2, array3, ...);

Example 1

<?php

$name1 = array(
	 'Enzo',
	 'Harlow Olivia Calliope',
	 'Jamison Leon');
$name2 = array(
	 'Romeo',
	 'Ronan',
	 'Benjamin',
	 'Wolfgang');
$name3 = array(
	 'Romeo Jon',
	 'Stephanie Rose');

$names = array_merge($name1, $name2, $name3);

foreach ($names as $n) {
	echo $n;
	echo '<br/>';
}
?>


Output

Enzo
Harlow Olivia Calliope
Jamison Leon
Romeo
Ronan
Benjamin
Wolfgang
Romeo Jon
Stephanie Rose

Example 2


<?php

$name1 = array(
	 'Enzo' => 12,
	 'Harlow Olivia Calliope' => 3,
	 'Jamison Leon' => 9);
$name2 = array(
	 'Romeo' => 3,
	 'Ronan' => 13,
	 'Benjamin' => 5,
	 'Wolfgang' => 8);
$name3 = array(
	 'Romeo Jon' => 7,
	 'Stephanie Rose' => 8);

$names = array_merge($name1, $name2, $name3);

print_r($names);
?>



Output


Array
(
    [Enzo] => 12
    [Harlow Olivia Calliope] => 3
    [Jamison Leon] => 9
    [Romeo] => 3
    [Ronan] => 13
    [Benjamin] => 5
    [Wolfgang] => 8
    [Romeo Jon] => 7
    [Stephanie Rose] => 8
)



Related Links


Navigation
Web
SQL
MISC
References