PHP array_merge() Function

Learn via video courses
Topics Covered

Overview

The array_merge() function is a fundamental tool for merging arrays, creating a new array that contains the values of all input arrays. This operation preserves keys and reindexes numeric keys, allowing you to merge both indexed and associative arrays seamlessly. In addition, the + operator can be used to merge arrays, but it retains keys from the first array and discards duplicate keys from subsequent arrays.

Syntax of array_merge() in PHP

The array_merge() function in PHP is used to merge two or more arrays into a single array. It creates a new array containing all the values from the input arrays. Here is the syntax of the array_merge() function:

array1, array2, array3, ...: The arrays you want to merge. You can pass as many arrays as needed, separated by commas.

Parameter Values of array_merge() in PHP

The array_merge() function in PHP is used to merge two or more arrays into a single array. It accepts multiple arrays as parameters and creates a new array that contains all the values from the input arrays. Here are the details of the parameter values for the array_merge() function:

  • array1, array2, array3, ...: These are the arrays you want to merge. You can provide two or more arrays as parameters, separated by commas. The function will combine the elements of these arrays into a single merged array.

Return Value of array_merge() in PHP

The array_merge() function in PHP returns a new array containing all the values from the input arrays. This merged array is a combination of elements from the provided arrays, with keys and values preserved. Here are the details of the return value:

  • New Merged Array:

    The function returns a new array that contains all the values from the input arrays. The keys from the input arrays are preserved, ensuring that the relationship between keys and values remains intact.

  • Key Preservation:

    String keys from the input arrays are maintained in the merged array. If two or more input arrays have the same string key, the later value will overwrite the earlier value in the merged array.

  • Numeric Key Reindexing:

    If the input arrays have numeric keys, those keys will be reindexed sequentially in the merged array. This reindexing ensures that the merged array has consecutive numeric keys starting from 0.

Examples of array_merge() in PHP

Simple array_merge() example

Here's a simple example of using the array_merge() function in PHP:

Output:

In this example, the array_merge() function combines the elements of both $array1 and $array2 into a new array called $mergedArray. The resulting array contains all the values from the input arrays, and numeric keys are reindexed sequentially.

Merge two associative arrays into one array

To merge two associative arrays into one array in PHP, you can use the array_merge() function. This function allows you to combine the elements of two or more arrays while preserving the keys. Here's how you can do it in detail:

Output:

In this example:

  • $array1 contains the key-value pairs "name" => "Alice" and "age" => 25.
  • $array2 contains the key-value pairs "city" => "New York" and "age" => 30.
  • When we merge these two arrays using array_merge(), the resulting $mergedArray contains all the key-value pairs from both input arrays. The value associated with the key "age" is taken from the second array ($array2), overwriting the value from the first array ($array1). The keys "name" and "city" are maintained, and the merged array preserves the original keys and values.

Using only one array parameter with integer keys

In PHP, the array_merge() function requires at least two arrays to be merged. However, if you have only one array and want to change the integer keys to sequential numeric keys, you can achieve this by reindexing the keys using the array_values() function. Here's how you can do it:

Output:

In this example:

  • The $associativeArray contains elements with integer keys (10, 20, 30) and corresponding values ("Apple", "Banana", "Orange").
  • We use the array_values() function to reindex the array, which essentially removes the original keys and assigns new sequential numeric keys (0, 1, 2) to the elements.

array_merge() with non-array types

In PHP, the array_merge() function is designed to merge arrays, and it cannot directly merge non-array types. However, you can use the array_merge() function to merge arrays containing non-array values. Here's how it works in detail:

Output:

In this example:

  • $array1 contains the values "a" and "b".
  • $array2 is a non-array value "c".
  • When using array_merge() with arrays containing non-array values, the non-array value is treated as a single-element array. In the output, the non-array value "c" is added as a separate element in the merged array. This behavior allows you to combine arrays and non-array values within a single merged array.

Conclusion

  • The array_merge() function allows you to merge two or more arrays into a single array, preserving keys and values.
  • String keys from input arrays are maintained in the merged array, ensuring that the relationship between keys and values remains intact.
  • If two or more input arrays have the same string key, the later value will overwrite the earlier value in the merged array.
  • If input arrays have numeric keys, those keys are reindexed sequentially in the merged array.
  • array_merge() works with both indexed and associative arrays, making it versatile for various data structures.
  • For merging arrays with non-array values or specific requirements, other techniques and functions like the + operator, array_values(), or custom methods can be used.