6 * Contains utility functions for array operations
8 * @copyright © 2004-2007 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @package squirrelmail
15 * Swaps two array values by position and maintain keys
17 * @param array $a (recursive) array
18 * @param mixed $v1 value 1
19 * @param mixed $v2 value 2
20 * @return bool $r true on success
21 * @author Marc Groot Koerkamp
23 function sqm_array_swap_values(&$a,$v1,$v2) {
25 if (in_array($v1,$a) && in_array($v2,$a)) {
26 $k1 = array_search($v1,$a);
27 $k2 = array_search($v1,$a);
38 * Move array value 2 array values by position and maintain keys
40 * @param array $a (recursive) array
41 * @param mixed $v value to move
42 * @param int $p positions to move
43 * @return bool $success
44 * @author Marc Groot Koerkamp
46 function sqm_array_kmove_value(&$a,$v,$p) {
48 $a_v = array_values($a);
49 $a_k = array_keys($a);
50 if (in_array($v, $a_v)) {
51 $k = array_search($v, $a_v);
53 if ($p_n > 0 && $p_n < count($a_v)) {
55 $a_v[$k] = $a_v[$p_n];
58 $a_k[$k] = $a_k[$p_n];
60 $r = array_combine($a_k, $a_v);
71 * Move array value 2 array values by position. Does not maintain keys
73 * @param array $a (recursive) array
74 * @param mixed $v value to move
75 * @param int $p positions to move
76 * @return bool $success
77 * @author Marc Groot Koerkamp
79 function sqm_array_move_value(&$a,$v,$p) {
81 $a_v = array_values($a);
82 if (in_array($v, $a_v)) {
83 $k = array_search($v, $a_v);
85 if ($p_n >= 0 && $p_n < count($a_v)) {
87 $a_v[$k] = $a_v[$p_n];
97 * Retrieve an array value n positions relative to a reference value.
99 * @param array $a array
100 * @param mixed $v reference value
101 * @param int $p offset to reference value in positions
102 * @return mixed $r false on failure (or if the found value is false)
103 * @author Marc Groot Koerkamp
105 function sqm_array_get_value_by_offset($a,$v,$p) {
107 $a_v = array_values($a);
108 if (in_array($v, $a_v)) {
109 $k = array_search($v, $a_v);
111 if ($p_n >= 0 && $p_n < count($a_v)) {
119 if (!function_exists('array_combine')) {
121 * Creates an array by using one array for keys and another for its values (PHP 5)
123 * @param array $aK array keys
124 * @param array $aV array values
125 * @return mixed $r combined array on success, false on failure
126 * @author Marc Groot Koerkamp
128 function array_combine($aK, $aV) {
132 if ($iCaK && $iCaV && $iCaK == $iCaV) {
134 for ($i=0;$i<$iCaK;++
$i) {
135 $aC[$aK[$i]] = $aV[$i];
145 * Merges two variables into a single array
147 * Similar to PHP array_merge function, but provides same
148 * functionality as array_merge without losing array values
149 * with same key names. If the values under identical array
150 * keys are both strings and $concat_strings is TRUE, those
151 * values are concatenated together, otherwise they are placed
152 * in a sub-array and are merged (recursively) in the same manner.
154 * If either of the elements being merged is not an array,
155 * it will simply be added to the returned array.
157 * If both values are strings and $concat_strings is TRUE,
158 * a concatenated string is returned instead of an array.
160 * @param mixed $a First element to be merged
161 * @param mixed $b Second element to be merged
162 * @param boolean $concat_strings Whether or not string values
163 * should be concatenated instead
164 * of added to different array
165 * keys (default TRUE)
167 * @return array The merged $a and $b in one array
170 * @author Paul Lesniewski
173 function sqm_array_merge($a, $b, $concat_strings=true) {
180 if (is_string($a) && is_string($b) && $concat_strings) {
188 foreach ($b as $key => $value) {
189 if (isset($ret[$key])) {
190 $ret[$key] = sqm_array_merge($ret[$key], $value, $concat_strings);