Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

php

What is the array_merge_recursive() in PHP?

NDUKWE CHIDERA K.

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code

Overview

An array is a data type in PHP that can accept any data type. The values held in an array can be accessed using an index or a key. The array_merge_recursive() method will join together the values of as many arrays possible, recursively. With this method, one or more arrays will be merged together in a manner such that the values of one will be appended to that of another, and so on until all the arrays involved are merged. The array_merge_recursive() function returns the resulting array after the merging.

Syntax

array_merge_recursive($array_1, $array_2, ....$array_nth)

Parameters

  • $array_1, $array_2, ....$array_nth: This is a list of arrays that will be merged. $array is the first on that list and $array_nth is the last in the list.

Some conditions

When two or more of the arrays to be merged have the same string keys, the values for these string keys will be merged together into an array, and this is done recursively. This recursive action is so that in a case where one of the array values is an array itself, the function will merge it with a corresponding entry in another array too. Also, if the arrays have the same numeric key, the later value will not overwrite the original value, but will be appended.

Return value

An array that contains all the merged array values will be returned.

Code

The code snippet below is a sample of how the function works.

<?php
//declare variables of nested arrays
$arrVal1 = array("color" => array("favorite" => "red"), 5);
$arrVal2 = array(10, "color" => array("favorite" => "green", "blue"));
//declare non nested arrays
$arrVal3 = array(10,"spoon","cups","trays", "kettle");
$arrVal4 = array("orange" ,"mango", "onions","favorite","green","blue");
$arrVal5 = ["pink" ,"red","brown","green","favorite", "grey", "black"];
//call the array_merge_recursive method
$result1 = array_merge_recursive($arrVal1, $arrVal2);
$result2 = array_merge_recursive($arrVal3, $arrVal4,$arrVal5);
echo "first merger \n";
var_dump($result1);
echo "second merger \n";
print_r($result2);
?>

Explanation

  • Lines 4 and 5: We declare some nested array values.

  • Lines 8–10: We declare some linear arrays.

  • Lines 13 and 14: We use the array_merge_recursive() function to merge the values of the earlier declared arrays and save the outcome to the $output1 and $output2 functions.

  • Lines 17 and 20: We use the echo method to make new lines so that the values from the function output can each print on new lines.

  • Lines 18 and 21: We display the arrays from merging the earlier declared arrays.

RELATED TAGS

php

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Answers Code
Keep Exploring