X-Git-Url: https://vcs.fsf.org/?p=squirrelmail.git;a=blobdiff_plain;f=functions%2Farrays.php;h=6de6c76f258fc1112541f40cb416fafd9d47da98;hp=bec4c986d84e5a6ffefab36383afaec1ff32c52d;hb=2d24b622c4c2e78a2e43ba1bed9877556de18de9;hpb=47ccfad452e8d345542d09e59112cac317cffed8 diff --git a/functions/arrays.php b/functions/arrays.php index bec4c986..6de6c76f 100644 --- a/functions/arrays.php +++ b/functions/arrays.php @@ -5,7 +5,7 @@ * * Contains utility functions for array operations * - * @copyright © 2004-2006 The SquirrelMail Project Team + * @copyright 2004-2017 The SquirrelMail Project Team * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @version $Id$ * @package squirrelmail @@ -139,3 +139,65 @@ if (!function_exists('array_combine')) { return $r; } } + + + /** + * Merges two variables into a single array + * + * Similar to PHP array_merge function, but provides same + * functionality as array_merge without losing array values + * with same key names. If the values under identical array + * keys are both strings and $concat_strings is TRUE, those + * values are concatenated together, otherwise they are placed + * in a sub-array and are merged (recursively) in the same manner. + * + * If either of the elements being merged is not an array, + * it will simply be added to the returned array. + * + * If both values are strings and $concat_strings is TRUE, + * a concatenated string is returned instead of an array. + * + * @param mixed $a First element to be merged + * @param mixed $b Second element to be merged + * @param boolean $concat_strings Whether or not string values + * should be concatenated instead + * of added to different array + * keys (default TRUE) + * + * @return array The merged $a and $b in one array + * + * @since 1.5.2 + * @author Paul Lesniewski + * + */ +function sqm_array_merge($a, $b, $concat_strings=true) { + + $ret = array(); + + if (is_array($a)) { + $ret = $a; + } else { + if (is_string($a) && is_string($b) && $concat_strings) { + return $a . $b; + } + $ret[] = $a; + } + + + if (is_array($b)) { + foreach ($b as $key => $value) { + if (isset($ret[$key])) { + $ret[$key] = sqm_array_merge($ret[$key], $value, $concat_strings); + } else { + $ret[$key] = $value; + } + } + } else { + $ret[] = $b; + } + + return $ret; + +} + +