Skip to content

Instantly share code, notes, and snippets.

@wojtha
Created May 31, 2011 20:10
Show Gist options
  • Save wojtha/1001173 to your computer and use it in GitHub Desktop.
Save wojtha/1001173 to your computer and use it in GitHub Desktop.
Drupal Array Merge - copied from swftools_array_merge().
<?php
/**
* Merges two multi-dimensional arrays.
*
* Taken from http://drupalcontrib.org/api/search/6/swftools_array_merge
*
* This function is used by players that filter their settings to strip out
* blanks or defaults. For the admin page we need a full set of values to prevent
* errors. Since the arrays might be multi-dimensional we cannot use a regular
* array_merge(). The values in the first array will be over-written by values in
* the second, if they exist.
*
* @param array $array1
* The first array to merge.
* @param array $array2
* The second array to merge.
*
* @return array
* The result of the merge.
*/
function drupal_array_merge($array1, $array2) {
// Iterate over $array 2 (this is normally the smaller of the two)
foreach ($array2 as $key => $value) {
// If this key is present in $array1 then work out what to do
if (isset($array1[$key])) {
// If both keys hold arrays, combine them
if (is_array($value) && is_array($array1[$key])) {
$array1[$key] = drupal_array_merge($array1[$key], $value);
}
else {
// Replace value in $array1 with that from $array2
$array1[$key] = $value;
}
}
else {
// Simply put this value in $array1 if it isn't already in $array1
$array1[$key] = $value;
}
}
// Return the result
return $array1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment