Merging Arrays

You want to merge two arrays into one.

Use array_merge(). It works with both predefined arrays and arrays defined in place using array().

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo "<p><b>Display fruits with numerical keys:</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
print_r($fruits);
$vegetables = array('Tomatoes', 'Peas');
echo "<p><b>Display vegetables with numerical keys:</b></p>";
print_r($vegetables);
$produce = array_merge($fruits, $vegetables);
echo "<p><b>Use array_merge() to append vegetables to fruits.</b></p>";
print_r($produce);

echo "<p><b>Display fruits with string keys:</b></p>";
$fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges');
print_r($fruits);
$vegetables = array('red2'=>'Tomatoes', 'green2'=>'Peas');
echo "<p><b>Display vegetables with string keys:</b></p>";
print_r($vegetables);
$produce = array_merge($fruits, $vegetables);
echo "<p><b>Use array_merge() to append vegetables to fruits.</b></p>";
print_r($produce);

$vegetables = array('red'=>'Tomatoes', 'green'=>'Peas');
echo "<p><b>Display new vegetables with same keys:</b></p>";
print_r($vegetables);
$produce = array_merge($fruits, $vegetables);
echo "<p><b>Use array_merge() to append new vegetables to fruits.</b></p>";
print_r($produce);

// Append a numerical array to an associative array
echo "<p><b>Display fruits:</b></p>";
$fruits = array('red'=>'Apples', 'green'=>'Grapes', 'yellow'=>'Bananas', 'orange'=>'Oranges');
print_r($fruits);
$fruits = array_merge($fruits, array('Pears'));
echo "<p><b>Append a numerical array to an associative array.</b></p>";
print_r($fruits);
?>
</p>
</body>
</html>

View the effect

You cannot use array_push() because PHP won't automatically flatten out the array into a series of independent variables, and you'll end up with a nested array.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo "<p><b>Display fruits with numerical keys:</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
print_r($fruits);
$vegetables = array('Tomatoes', 'Peas');
echo "<p><b>Display vegetables with numerical keys:</b></p>";
print_r($vegetables);
array_push($fruits, $vegetables);
echo "<p><b>Use array_push() to append vegetables to fruits.</b></p>";
print_r($fruits);
?>
</p>
</body>
</html>

View the effect

The + operator can also merge arrays. The array on the right does not overwrite any identically named keys found on the left. It doesn't do any reordering to prevent collisions.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
echo "<p><b>Display fruits with numerical keys:</b></p>";
$fruits = array('Apples', 'Grapes', 'Bananas', 'Oranges');
print_r($fruits);
$vegetables = array('Tomatoes', 'Peas');
echo "<p><b>Display vegetables with numerical keys:</b></p>";
print_r($vegetables);
echo "<p><b>Add vegetables to fruits.</b></p>";
print_r($fruits + $vegetables);
echo "<p><b>Add fruits to vegetables.</b></p>";
print_r($vegetables+$fruits);
?>
</p>
</body>
</html>

View the effect


Arrays (cintinued) | Adding all Values in an Array | Flipping an Array | Extracting Variables from Arrays | Imploding Arrays | Exploding Arrays | Navigating through Arrays | Merging Arrays | Comparing Arrays | Sorting Arrays | Creating Multidimensional Arrays | Sorting Multiple Arrays
© 2008: Hann So
email: hso@voyager.deanza.edu