Current Location: Home> Latest Articles> How to Use array_replace to Merge Multiple Arrays? A Detailed PHP Practical Guide

How to Use array_replace to Merge Multiple Arrays? A Detailed PHP Practical Guide

gitbox 2025-09-10

How to Use array_replace to Merge Multiple Arrays? A Detailed PHP Practical Guide

In PHP programming, working with arrays is a very common task. Often, we need to merge arrays, replace elements within arrays, or combine elements from multiple arrays. In these situations, the array_replace() function is a highly useful tool. It not only replaces values within an array but can also merge multiple arrays together.

This article will provide a detailed explanation of how to use the array_replace() function, helping you better understand how to merge multiple arrays in PHP.

What is array_replace?

array_replace() is a built-in PHP function used to merge elements from one or more arrays into the first array. If arrays contain the same key, the values from the later arrays will replace the values of the same key in the first array. In other words, it sequentially replaces elements in the earlier array with those from subsequent arrays.

Function Syntax

<span><span><span class="hljs-title function_ invoke__">array_replace</span></span><span>(</span><span><span class="hljs-keyword">array</span></span><span> </span><span><span class="hljs-variable">$array1</span></span><span>, </span><span><span class="hljs-keyword">array</span></span><span> ...</span><span><span class="hljs-variable">$arrays</span></span><span>): </span><span><span class="hljs-keyword">array</span></span><span>  
</span></span>
  • $array1: The first array, used as the target array, whose elements may be replaced by elements from other arrays.

  • $arrays: One or more arrays whose values will replace the corresponding values in $array1.

Examples

Basic Example

Suppose we have two arrays, and we want to merge the values of the second array into the first array:

... (examples remain unchanged, only explanation translated) ...

Output:

...

In this example, array_replace() replaced the value of the same key ('b') in $array1 with the value from $array2, and also added the new key-value pair ('c' => 4) from $array2 into the result array.

Merging Multiple Arrays

array_replace() also supports passing multiple arrays, allowing you to merge elements from several arrays.

...

Output:

...

In this example, the value from $array2 replaced the corresponding key ('b') in $array1, while the key-value pairs from $array3 were directly added to the result array.

Difference Between array_replace and array_merge

Although array_replace() can be used to merge multiple arrays, it behaves differently from the array_merge() function.

  1. array_replace(): It replaces values in the original array based on keys. If the same key exists, the later array will overwrite the corresponding key in the previous array.

  2. array_merge(): It merges arrays regardless of whether the keys are the same. For indexed arrays, it reindexes the keys; for associative arrays, it preserves the original keys.

For example:

...

Output:

...

When using array_merge(), the numeric keys are reindexed, while with array_replace(), the keys and indexes remain unchanged.

Things to Note

  • array_replace() only replaces keys that exist in the original array. For unmatched keys, subsequent arrays will simply add them into the result.

  • If arrays have numeric keys, array_replace() will replace values based on the keys, which may change the order of numeric indexes. In contrast, array_merge() will reindex numeric keys.

Summary

array_replace() is a very powerful function, suitable for replacing existing elements in an array or merging elements from multiple arrays into one. When working with arrays in PHP, mastering the use of array_replace() can make your code cleaner and more efficient.

If you need to handle merging multiple arrays while paying attention to key replacement and addition, array_replace() is definitely one of the essential tools at your disposal.