PHP: array_merge_ recursive() function

PHP: Merge two or more arrays recursively

The array_merge_recursive() function is used to merge the elements of one or more arrays together. The elements of one are appended to the end of the previous one.

If the input arrays have matching string keys, then the values for these keys are merged together into an array, and this is done recursively, so that if one of the values is an array itself, the function will merge it with a corresponding entry in another array too.

If the input arrays contain numeric keys, the later value will be appended instead of overriding the original value.


(PHP 4 and above)


array_merge_recursive(array_name1, array_name2...) 


Name Description Required /
array_name1 Specifies the name of the array. Required Array
array_name2.. Specifies the name of the array. Optional Array

Return value:

The merged array.

Value Type: Array


$array1=array("Subject" => "Physics","Chemistry", "Biology");
$array2=array("Class-XI", "Class-XII", "Roll"=>20);
$result=array_merge_recursive($array1, $array2);


Array ( [Subject] => Physics [0] => Chemistry [1] => Biology [2] => Class-XI [3] => Class-XII [Roll] => 20 ) 

View the example in the browser

Practice here online :

See also

PHP Function Reference

Previous: array_map
Next: array_merge

Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

PHP: Correct file permissions for WordPress

When you setup WP you (the webserver) may need write access to the files. So the access rights may need to be loose.

chown www-data:www-data  -R * # Let Apache be owner
find . -type d -exec chmod 755 {} \;  # Change directory permissions rwxr-xr-x
find . -type f -exec chmod 644 {} \;  # Change file permissions rw-r--r-

After the setup you should tighten the access rights, according to Hardening WordPress all files except for wp-content should be writable by your user account only. wp-content must be writable by www-data too.

chown <username>:<username>  -R * # Let your useraccount be owner
chown www-data:www-data wp-content # Let apache be owner of wp-content

Maybe you want to change the contents in wp-content later on. In this case you could

  • temporarily change to the user to www-data with su,
  • give wp-content group write access 775 and join the group www-data or
  • give your user the access rights to the folder using ACLs.

Whatever you do, make sure the files have rw permissions for www-data.

Ref : https://bit.ly/3hcrTkL


We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook