summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMark Carver2013-01-16 07:39:44 (GMT)
committer Mark Carver2013-01-16 07:39:44 (GMT)
commit73a824397a45d808af8e339b225081184834f07f (patch)
tree9269a2d1354a371aca610adb3daaecdc5b55a5ff
parentf4389f9d2b895337ecffbccb25a2cbb6c72403b0 (diff)
Added array_merge_recursive_distinct() function to utilities.inc.
-rw-r--r--includes/utilities.inc36
1 files changed, 36 insertions, 0 deletions
diff --git a/includes/utilities.inc b/includes/utilities.inc
index df48c85..0eee637 100644
--- a/includes/utilities.inc
+++ b/includes/utilities.inc
@@ -295,3 +295,39 @@ function advagg_string_ends_with($haystack, $needle) {
}
return substr_compare($haystack, $needle, $haystack_len -$needle_len, $needle_len, TRUE) === 0;
}
+
+if (!function_exists('array_merge_recursive_distinct')) {
+ /**
+ * Merges any number of arrays / parameters recursively, replacing
+ * entries with string keys with values from latter arrays.
+ * If the entry or the next value to be assigned is an array, then it
+ * automagically treats both arguments as an array.
+ * Numeric entries are appended, not replaced, but only if they are
+ * unique
+ *
+ * calling: result = array_merge_recursive_distinct(a1, a2, ... aN)
+ **/
+
+ function array_merge_recursive_distinct () {
+ $arrays = func_get_args();
+ $base = array_shift($arrays);
+ if(!is_array($base)) $base = empty($base) ? array() : array($base);
+ foreach($arrays as $append) {
+ if(!is_array($append)) $append = array($append);
+ foreach($append as $key => $value) {
+ if(!array_key_exists($key, $base) and !is_numeric($key)) {
+ $base[$key] = $append[$key];
+ continue;
+ }
+ if(is_array($value) or is_array($base[$key])) {
+ $base[$key] = array_merge_recursive_distinct($base[$key], $append[$key]);
+ } else if(is_numeric($key)) {
+ if(!in_array($value, $base)) $base[] = $value;
+ } else {
+ $base[$key] = $value;
+ }
+ }
+ }
+ return $base;
+ }
+}