<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'en',
  ),
  'this' => 
  array (
    0 => 'function.array-replace.php',
    1 => 'array_replace',
    2 => 'Replaces elements from passed arrays into the first array',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => 'Array Functions',
  ),
  'prev' => 
  array (
    0 => 'function.array-reduce.php',
    1 => 'array_reduce',
  ),
  'next' => 
  array (
    0 => 'function.array-replace-recursive.php',
    1 => 'array_replace_recursive',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'en',
    'path' => 'reference/array/functions/array-replace.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.array-replace" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">array_replace</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">array_replace</span> &mdash; <span class="dc-title">Replaces elements from passed arrays into the first array</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.array-replace-description">
  <h3 class="title">Description</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>array_replace</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">$array</code></span>, <span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter">...$replacements</code></span>): <span class="type"><a href="language.types.array.php" class="type array">array</a></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>array_replace()</strong></span> creates a new array and assigns items into
   it for each key in each of the provided arrays. If a key appears in multiple
   input arrays, the value from the right-most input array will be used.
  </p>
  <p class="para">
   <span class="function"><strong>array_replace()</strong></span> does not process elements items recursively,
   it replaces the entire value for each key when it does a replacement.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.array-replace-parameters">
  <h3 class="title">Parameters</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="para">
       The array in which elements are replaced.
      </p>
     </dd>
    
    
     <dt><code class="parameter">replacements</code></dt>
     <dd>
      <p class="para">
       Arrays from which elements will be extracted.
       Values from later arrays overwrite the previous values.
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.array-replace-returnvalues">
  <h3 class="title">Return Values</h3>
  <p class="para">
   Returns an <span class="type"><a href="language.types.array.php" class="type array">array</a></span>.
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.array-replace-examples">
  <h3 class="title">Examples</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>Example #1 <span class="function"><strong>array_replace()</strong></span> example</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$base </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"orange"</span><span style="color: #007700">, </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">, </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">, </span><span style="color: #DD0000">"raspberry"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$replacements </span><span style="color: #007700">= array(</span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"pineapple"</span><span style="color: #007700">, </span><span style="color: #0000BB">4 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"cherry"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$replacements2 </span><span style="color: #007700">= array(</span><span style="color: #0000BB">0 </span><span style="color: #007700">=&gt; </span><span style="color: #DD0000">"grape"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$basket </span><span style="color: #007700">= </span><span style="color: #0000BB">array_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">, </span><span style="color: #0000BB">$replacements</span><span style="color: #007700">, </span><span style="color: #0000BB">$replacements2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$basket</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">array(5) {
  [0]=&gt;
  string(5) &quot;grape&quot;
  [1]=&gt;
  string(6) &quot;banana&quot;
  [2]=&gt;
  string(5) &quot;apple&quot;
  [3]=&gt;
  string(9) &quot;raspberry&quot;
  [4]=&gt;
  string(6) &quot;cherry&quot;
}</pre>
</div>
    </div>
   </div>
   <div class="example" id="example-2">
    <p><strong>Example #2 Example of how nested arrays are handled</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$base </span><span style="color: #007700">= [ </span><span style="color: #DD0000">'citrus' </span><span style="color: #007700">=&gt; [ </span><span style="color: #DD0000">'orange'</span><span style="color: #007700">, </span><span style="color: #DD0000">'lemon' </span><span style="color: #007700">], </span><span style="color: #DD0000">'pome' </span><span style="color: #007700">=&gt; [ </span><span style="color: #DD0000">'apple' </span><span style="color: #007700">] ];<br /></span><span style="color: #0000BB">$replacements </span><span style="color: #007700">= [ </span><span style="color: #DD0000">'citrus' </span><span style="color: #007700">=&gt; [ </span><span style="color: #DD0000">'grapefruit' </span><span style="color: #007700">] ];<br /></span><span style="color: #0000BB">$replacements2 </span><span style="color: #007700">= [ </span><span style="color: #DD0000">'citrus' </span><span style="color: #007700">=&gt; [ </span><span style="color: #DD0000">'kumquat'</span><span style="color: #007700">, </span><span style="color: #DD0000">'citron' </span><span style="color: #007700">], </span><span style="color: #DD0000">'pome' </span><span style="color: #007700">=&gt; [ </span><span style="color: #DD0000">'loquat' </span><span style="color: #007700">] ];<br /><br /></span><span style="color: #0000BB">$basket </span><span style="color: #007700">= </span><span style="color: #0000BB">array_replace</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">, </span><span style="color: #0000BB">$replacements</span><span style="color: #007700">, </span><span style="color: #0000BB">$replacements2</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$basket</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

    <div class="example-contents"><p>The above example will output:</p></div>
    <div class="example-contents screen">
<div class="annotation-interactive examplescode"><pre class="examplescode">array(2) {
  [&quot;citrus&quot;]=&gt;
  array(2) {
    [0]=&gt;
    string(7) &quot;kumquat&quot;
    [1]=&gt;
    string(6) &quot;citron&quot;
  }
  [&quot;pome&quot;]=&gt;
  array(1) {
    [0]=&gt;
    string(6) &quot;loquat&quot;
  }
}</pre>
</div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.array-replace-seealso">
  <h3 class="title">See Also</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="function.array-replace-recursive.php" class="function" rel="rdfs-seeAlso">array_replace_recursive()</a> - Replaces elements from passed arrays into the first array recursively</span></li>
    <li><span class="function"><a href="function.array-merge.php" class="function" rel="rdfs-seeAlso">array_merge()</a> - Merge one or more arrays</span></li>
   </ul>
  </p>
 </div>

</div><?php manual_footer($setup); ?>