Remove ancient code that tried to set the default identity to match the FROM header...
[squirrelmail.git] / functions / arrays.php
1 <?php
2
3 /**
4 * arrays.php
5 *
6 * Contains utility functions for array operations
7 *
8 * @copyright 2004-2012 The SquirrelMail Project Team
9 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 * @version $Id$
11 * @package squirrelmail
12 */
13
14 /**
15 * Swaps two array values by position and maintain keys
16 *
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
22 */
23 function sqm_array_swap_values(&$a,$v1,$v2) {
24 $r = false;
25 if (in_array($v1,$a) && in_array($v2,$a)) {
26 $k1 = array_search($v1,$a);
27 $k2 = array_search($v1,$a);
28 $d = $a[$k1];
29 $a[$k1] = $a[$k2];
30 $a[$k2] = $d;
31 $r = true;
32 }
33 return $r;
34 }
35
36
37 /**
38 * Move array value 2 array values by position and maintain keys
39 *
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
45 */
46 function sqm_array_kmove_value(&$a,$v,$p) {
47 $r = false;
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);
52 $p_n = $k + $p;
53 if ($p_n > 0 && $p_n < count($a_v)) {
54 $d = $a_v[$k];
55 $a_v[$k] = $a_v[$p_n];
56 $a_v[$p_n] = $d;
57 $d = $a_k[$k];
58 $a_k[$k] = $a_k[$p_n];
59 $a_k[$p_n] = $d;
60 $r = array_combine($a_k, $a_v);
61 if ($a !== false) {
62 $a = $r;
63 $r = true;
64 }
65 }
66 }
67 return $r;
68 }
69
70 /**
71 * Move array value 2 array values by position. Does not maintain keys
72 *
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
78 */
79 function sqm_array_move_value(&$a,$v,$p) {
80 $r = false;
81 $a_v = array_values($a);
82 if (in_array($v, $a_v)) {
83 $k = array_search($v, $a_v);
84 $p_n = $k + $p;
85 if ($p_n >= 0 && $p_n < count($a_v)) {
86 $d = $a_v[$k];
87 $a_v[$k] = $a_v[$p_n];
88 $a_v[$p_n] = $d;
89 $a = $a_v;
90 $r = true;
91 }
92 }
93 return $r;
94 }
95
96 /**
97 * Retrieve an array value n positions relative to a reference value.
98 *
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
104 */
105 function sqm_array_get_value_by_offset($a,$v,$p) {
106 $r = false;
107 $a_v = array_values($a);
108 if (in_array($v, $a_v)) {
109 $k = array_search($v, $a_v);
110 $p_n = $k + $p;
111 if ($p_n >= 0 && $p_n < count($a_v)) {
112 $r = $a_v[$p_n];
113 }
114 }
115 return $r;
116 }
117
118
119 if (!function_exists('array_combine')) {
120 /**
121 * Creates an array by using one array for keys and another for its values (PHP 5)
122 *
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
127 */
128 function array_combine($aK, $aV) {
129 $r = false;
130 $iCaK = count($aK);
131 $iCaV = count($aV);
132 if ($iCaK && $iCaV && $iCaK == $iCaV) {
133 $aC = array();
134 for ($i=0;$i<$iCaK;++$i) {
135 $aC[$aK[$i]] = $aV[$i];
136 }
137 $r = $aC;
138 }
139 return $r;
140 }
141 }
142
143
144 /**
145 * Merges two variables into a single array
146 *
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.
153 *
154 * If either of the elements being merged is not an array,
155 * it will simply be added to the returned array.
156 *
157 * If both values are strings and $concat_strings is TRUE,
158 * a concatenated string is returned instead of an array.
159 *
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)
166 *
167 * @return array The merged $a and $b in one array
168 *
169 * @since 1.5.2
170 * @author Paul Lesniewski
171 *
172 */
173 function sqm_array_merge($a, $b, $concat_strings=true) {
174
175 $ret = array();
176
177 if (is_array($a)) {
178 $ret = $a;
179 } else {
180 if (is_string($a) && is_string($b) && $concat_strings) {
181 return $a . $b;
182 }
183 $ret[] = $a;
184 }
185
186
187 if (is_array($b)) {
188 foreach ($b as $key => $value) {
189 if (isset($ret[$key])) {
190 $ret[$key] = sqm_array_merge($ret[$key], $value, $concat_strings);
191 } else {
192 $ret[$key] = $value;
193 }
194 }
195 } else {
196 $ret[] = $b;
197 }
198
199 return $ret;
200
201 }
202
203