Created
May 31, 2011 20:10
-
-
Save wojtha/1001173 to your computer and use it in GitHub Desktop.
Drupal Array Merge - copied from swftools_array_merge().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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