Skip to content

Instantly share code, notes, and snippets.

@liayn
Created May 8, 2016 12:38
Show Gist options
  • Save liayn/f03f0d98f4f5155d5ad535ab6b818ee6 to your computer and use it in GitHub Desktop.
Save liayn/f03f0d98f4f5155d5ad535ab6b818ee6 to your computer and use it in GitHub Desktop.
<?php
/**
* Merges two arrays recursively and "binary safe" (integer keys are
* overridden as well), overruling similar values in the first array
* ($arr0) with the values of the second array ($arr1)
* In case of identical keys, ie. keeping the values of the second.
*
* @param array $arr0 First array
* @param array $arr1 Second array, overruling the first array
* @param boolean $notAddKeys If set, keys that are NOT found in $arr0 (first array) will not be set.
* Thus only existing value can/will be overruled from second array.
* @param boolean $includeEmptyValues If set, values from $arr1 will overrule if they are
* empty or zero. Default: TRUE
* @param boolean $enableUnsetFeature If set, special values "__UNSET" can be used in the second array
* in order to unset array keys in the resulting array.
* @return array Resulting array where $arr1 values has overruled $arr0 values
*/
public static function arrayMergeRecursiveOverrule(
array $arr0,
array $arr1,
$notAddKeys = false,
$includeEmptyValues = true,
$enableUnsetFeature = true
) {
if (class_exists('\TYPO3\CMS\Core\Utility\GeneralUtility')) {
if (method_exists('\TYPO3\CMS\Core\Utility\ArrayUtility', 'mergeRecursiveWithOverrule')) {
call_user_func_array(
array('\TYPO3\CMS\Core\Utility\ArrayUtility', 'mergeRecursiveWithOverrule'),
array(
&$arr0,
$arr1,
$notAddKeys,
$includeEmptyValues,
$enableUnsetFeature
)
);
return $arr0;
} else {
return call_user_func(
array('\TYPO3\CMS\Core\Utility\GeneralUtility', 'array_merge_recursive_overrule'),
$arr0,
$arr1,
$notAddKeys,
$includeEmptyValues,
$enableUnsetFeature
);
}
} else {
return call_user_func(
array('t3lib_div', 'array_merge_recursive_overrule'),
$arr0,
$arr1,
$notAddKeys,
$includeEmptyValues,
$enableUnsetFeature
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment