Chris@0: 'x', 'attributes' => array('title' => t('X'), 'class' => array('a', 'b'))); Chris@0: * $link_options_2 = array('fragment' => 'y', 'attributes' => array('title' => t('Y'), 'class' => array('c', 'd'))); Chris@0: * Chris@0: * // This results in array('fragment' => array('x', 'y'), 'attributes' => Chris@0: * // array('title' => array(t('X'), t('Y')), 'class' => array('a', 'b', Chris@0: * // 'c', 'd'))). Chris@0: * $incorrect = array_merge_recursive($link_options_1, $link_options_2); Chris@0: * Chris@0: * // This results in array('fragment' => 'y', 'attributes' => Chris@0: * // array('title' => t('Y'), 'class' => array('a', 'b', 'c', 'd'))). Chris@0: * $correct = NestedArray::mergeDeep($link_options_1, $link_options_2); Chris@0: * @endcode Chris@0: * Chris@0: * @param array ... Chris@0: * Arrays to merge. Chris@0: * Chris@0: * @return array Chris@0: * The merged array. Chris@0: * Chris@0: * @see NestedArray::mergeDeepArray() Chris@0: */ Chris@0: public static function mergeDeep() Chris@0: { Chris@0: return self::mergeDeepArray(func_get_args()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Merges multiple arrays, recursively, and returns the merged array. Chris@0: * Chris@0: * This function is equivalent to NestedArray::mergeDeep(), except the Chris@0: * input arrays are passed as a single array parameter rather than Chris@0: * a variable parameter list. Chris@0: * Chris@0: * The following are equivalent: Chris@0: * - NestedArray::mergeDeep($a, $b); Chris@0: * - NestedArray::mergeDeepArray(array($a, $b)); Chris@0: * Chris@0: * The following are also equivalent: Chris@0: * - call_user_func_array('NestedArray::mergeDeep', $arrays_to_merge); Chris@0: * - NestedArray::mergeDeepArray($arrays_to_merge); Chris@0: * Chris@0: * @param array $arrays Chris@0: * An arrays of arrays to merge. Chris@0: * @param bool $preserveIntegerKeys Chris@0: * (optional) If given, integer keys will be preserved and merged Chris@0: * instead of appended. Defaults to false. Chris@0: * Chris@0: * @return array Chris@0: * The merged array. Chris@0: * Chris@0: * @see NestedArray::mergeDeep() Chris@0: */ Chris@0: public static function mergeDeepArray( Chris@0: array $arrays, Chris@0: $preserveIntegerKeys = false Chris@0: ) { Chris@0: $result = array(); Chris@0: foreach ($arrays as $array) { Chris@0: foreach ($array as $key => $value) { Chris@0: // Renumber integer keys as array_merge_recursive() does Chris@0: // unless $preserveIntegerKeys is set to TRUE. Note that PHP Chris@0: // automatically converts array keys that are integer strings Chris@0: // (e.g., '1') to integers. Chris@0: if (is_integer($key) && !$preserveIntegerKeys) { Chris@0: $result[] = $value; Chris@0: } elseif (isset($result[$key]) && Chris@0: is_array($result[$key]) && Chris@0: is_array($value) Chris@0: ) { Chris@0: // Recurse when both values are arrays. Chris@0: $result[$key] = self::mergeDeepArray( Chris@0: array($result[$key], $value), Chris@0: $preserveIntegerKeys Chris@0: ); Chris@0: } else { Chris@0: // Otherwise, use the latter value, overriding any Chris@0: // previous value. Chris@0: $result[$key] = $value; Chris@0: } Chris@0: } Chris@0: } Chris@0: return $result; Chris@0: } Chris@0: } Chris@0: // vim:sw=4:ts=4:sts=4:et: