X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=functions%2Farrays.php;h=57eb30a2011a8ad1c66c6cd13d65f5f9b6970597;hb=8fea605cde592038b1472a25c8ccf49acd88d3b4;hp=32a52cdc386d8bde834cc6ab6c499202f4970e4f;hpb=6c84ba1ec45ab854c37b6f65c5b4d84ab1c7aad4;p=squirrelmail.git diff --git a/functions/arrays.php b/functions/arrays.php index 32a52cdc..57eb30a2 100644 --- a/functions/arrays.php +++ b/functions/arrays.php @@ -3,11 +3,10 @@ /** * arrays.php * - * Copyright (c) 2004-2005 The SquirrelMail Project Team - * Licensed under the GNU GPL. For full terms see the file COPYING. - * * Contains utility functions for array operations * + * @copyright © 2004-2006 The SquirrelMail Project Team + * @license http://opensource.org/licenses/gpl-license.php GNU Public License * @version $Id$ * @package squirrelmail */ @@ -41,7 +40,7 @@ * @param array $a (recursive) array * @param mixed $v value to move * @param int $p positions to move - * @return bool $succes + * @return bool $success * @author Marc Groot Koerkamp */ function sqm_array_kmove_value(&$a,$v,$p) { @@ -74,7 +73,7 @@ function sqm_array_kmove_value(&$a,$v,$p) { * @param array $a (recursive) array * @param mixed $v value to move * @param int $p positions to move - * @return bool $succes + * @return bool $success * @author Marc Groot Koerkamp */ function sqm_array_move_value(&$a,$v,$p) { @@ -123,7 +122,7 @@ if (!function_exists('array_combine')) { * * @param array $aK array keys * @param array $aV array values - * @return mixed $r combined array on succes, false on failure + * @return mixed $r combined array on success, false on failure * @author Marc Groot Koerkamp */ function array_combine($aK, $aV) { @@ -139,4 +138,66 @@ if (!function_exists('array_combine')) { } return $r; } -} \ No newline at end of file +} + + + /** + * 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; + +} + +