<HTML ><HEAD ><TITLE >array_merge</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Array Functions" HREF="ref.array.html"><LINK REL="PREVIOUS" TITLE="array_merge_recursive" HREF="function.array-merge-recursive.html"><LINK REL="NEXT" TITLE="array_multisort" HREF="function.array-multisort.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.array-merge-recursive.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.array-multisort.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.array-merge" ></A >array_merge</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN7710" ></A ><P > (PHP 4 )</P >array_merge -- Merge two or more arrays</DIV ><DIV CLASS="refsect1" ><A NAME="AEN7713" ></A ><H2 >Description</H2 >array <B CLASS="methodname" >array_merge</B > ( array array1, array array2 [, array ...])<BR ></BR ><P > <B CLASS="function" >array_merge()</B > merges the elements of two or more arrays together so that the values of one are appended to the end of the previous one. It returns the resulting array. </P ><P > If the input arrays have the same string keys, then the later value for that key will overwrite the previous one. If, however, the arrays contain numeric keys, the later value will <SPAN CLASS="strong" ><B CLASS="emphasis" >not</B ></SPAN > overwrite the original value, but will be appended. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7732" ></A ><P ><B >Example 1. <B CLASS="function" >array_merge()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$array1 = array ("color" => "red", 2, 4); $array2 = array ("a", "b", "color" => "green", "shape" => "trapezoid", 4); $result = array_merge ($array1, $array2);</PRE ></TD ></TR ></TABLE ><P > The <TT CLASS="literal" >$result</TT > will be: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [color] => green [0] => 2 [1] => 4 [2] => a [3] => b [shape] => trapezoid [4] => 4 )</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN7740" ></A ><P ><B >Example 2. Simple <B CLASS="function" >array_merge()</B > example</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$array1 = array(); $array2 = array(1 => "data"); $result = array_merge($array1, $array2);</PRE ></TD ></TR ></TABLE ><P > Don't forget that numeric keys will be renumbered! <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [0] => data )</PRE ></TD ></TR ></TABLE > </P ><P > If you want to completely preserve the arrays and just want to append them to each other, use the <TT CLASS="literal" >+</TT > operator: <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >$array1 = array(); $array2 = array(1 => "data"); $result = $array1 + $array2;</PRE ></TD ></TR ></TABLE > The numeric key will be preserved and thus the association remains. <TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >Array ( [1] => data )</PRE ></TD ></TR ></TABLE > </P ></DIV ></TD ></TR ></TABLE > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B > Shared keys will be overwritten on a first-come first-served basis. </P ></BLOCKQUOTE ></DIV ><P > See also <A HREF="function.array-merge-recursive.html" ><B CLASS="function" >array_merge_recursive()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.array-merge-recursive.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.array-multisort.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >array_merge_recursive</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.array.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >array_multisort</TD ></TR ></TABLE ></DIV ></BODY ></HTML >