arrays.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * arrays.php
  4. *
  5. * Contains utility functions for array operations
  6. *
  7. * @copyright 2004-2021 The SquirrelMail Project Team
  8. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  9. * @version $Id$
  10. * @package squirrelmail
  11. */
  12. /**
  13. * Swaps two array values by position and maintain keys
  14. *
  15. * @param array $a (recursive) array
  16. * @param mixed $v1 value 1
  17. * @param mixed $v2 value 2
  18. * @return bool $r true on success
  19. * @author Marc Groot Koerkamp
  20. */
  21. function sqm_array_swap_values(&$a,$v1,$v2) {
  22. $r = false;
  23. if (in_array($v1,$a) && in_array($v2,$a)) {
  24. $k1 = array_search($v1,$a);
  25. $k2 = array_search($v1,$a);
  26. $d = $a[$k1];
  27. $a[$k1] = $a[$k2];
  28. $a[$k2] = $d;
  29. $r = true;
  30. }
  31. return $r;
  32. }
  33. /**
  34. * Move array value 2 array values by position and maintain keys
  35. *
  36. * @param array $a (recursive) array
  37. * @param mixed $v value to move
  38. * @param int $p positions to move
  39. * @return bool $success
  40. * @author Marc Groot Koerkamp
  41. */
  42. function sqm_array_kmove_value(&$a,$v,$p) {
  43. $r = false;
  44. $a_v = array_values($a);
  45. $a_k = array_keys($a);
  46. if (in_array($v, $a_v)) {
  47. $k = array_search($v, $a_v);
  48. $p_n = $k + $p;
  49. if ($p_n > 0 && $p_n < count($a_v)) {
  50. $d = $a_v[$k];
  51. $a_v[$k] = $a_v[$p_n];
  52. $a_v[$p_n] = $d;
  53. $d = $a_k[$k];
  54. $a_k[$k] = $a_k[$p_n];
  55. $a_k[$p_n] = $d;
  56. $r = array_combine($a_k, $a_v);
  57. if ($a !== false) {
  58. $a = $r;
  59. $r = true;
  60. }
  61. }
  62. }
  63. return $r;
  64. }
  65. /**
  66. * Move array value 2 array values by position. Does not maintain keys
  67. *
  68. * @param array $a (recursive) array
  69. * @param mixed $v value to move
  70. * @param int $p positions to move
  71. * @return bool $success
  72. * @author Marc Groot Koerkamp
  73. */
  74. function sqm_array_move_value(&$a,$v,$p) {
  75. $r = false;
  76. $a_v = array_values($a);
  77. if (in_array($v, $a_v)) {
  78. $k = array_search($v, $a_v);
  79. $p_n = $k + $p;
  80. if ($p_n >= 0 && $p_n < count($a_v)) {
  81. $d = $a_v[$k];
  82. $a_v[$k] = $a_v[$p_n];
  83. $a_v[$p_n] = $d;
  84. $a = $a_v;
  85. $r = true;
  86. }
  87. }
  88. return $r;
  89. }
  90. /**
  91. * Retrieve an array value n positions relative to a reference value.
  92. *
  93. * @param array $a array
  94. * @param mixed $v reference value
  95. * @param int $p offset to reference value in positions
  96. * @return mixed $r false on failure (or if the found value is false)
  97. * @author Marc Groot Koerkamp
  98. */
  99. function sqm_array_get_value_by_offset($a,$v,$p) {
  100. $r = false;
  101. $a_v = array_values($a);
  102. if (in_array($v, $a_v)) {
  103. $k = array_search($v, $a_v);
  104. $p_n = $k + $p;
  105. if ($p_n >= 0 && $p_n < count($a_v)) {
  106. $r = $a_v[$p_n];
  107. }
  108. }
  109. return $r;
  110. }
  111. if (!function_exists('array_combine')) {
  112. /**
  113. * Creates an array by using one array for keys and another for its values (PHP 5)
  114. *
  115. * @param array $aK array keys
  116. * @param array $aV array values
  117. * @return mixed $r combined array on success, false on failure
  118. * @author Marc Groot Koerkamp
  119. */
  120. function array_combine($aK, $aV) {
  121. $r = false;
  122. $iCaK = count($aK);
  123. $iCaV = count($aV);
  124. if ($iCaK && $iCaV && $iCaK == $iCaV) {
  125. $aC = array();
  126. for ($i=0;$i<$iCaK;++$i) {
  127. $aC[$aK[$i]] = $aV[$i];
  128. }
  129. $r = $aC;
  130. }
  131. return $r;
  132. }
  133. }
  134. /**
  135. * Merges two variables into a single array
  136. *
  137. * Similar to PHP array_merge function, but provides same
  138. * functionality as array_merge without losing array values
  139. * with same key names. If the values under identical array
  140. * keys are both strings and $concat_strings is TRUE, those
  141. * values are concatenated together, otherwise they are placed
  142. * in a sub-array and are merged (recursively) in the same manner.
  143. *
  144. * If either of the elements being merged is not an array,
  145. * it will simply be added to the returned array.
  146. *
  147. * If both values are strings and $concat_strings is TRUE,
  148. * a concatenated string is returned instead of an array.
  149. *
  150. * @param mixed $a First element to be merged
  151. * @param mixed $b Second element to be merged
  152. * @param boolean $concat_strings Whether or not string values
  153. * should be concatenated instead
  154. * of added to different array
  155. * keys (default TRUE)
  156. *
  157. * @return array The merged $a and $b in one array
  158. *
  159. * @since 1.5.2
  160. * @author Paul Lesniewski
  161. *
  162. */
  163. function sqm_array_merge($a, $b, $concat_strings=true) {
  164. $ret = array();
  165. if (is_array($a)) {
  166. $ret = $a;
  167. } else {
  168. if (is_string($a) && is_string($b) && $concat_strings) {
  169. return $a . $b;
  170. }
  171. $ret[] = $a;
  172. }
  173. if (is_array($b)) {
  174. foreach ($b as $key => $value) {
  175. if (isset($ret[$key])) {
  176. $ret[$key] = sqm_array_merge($ret[$key], $value, $concat_strings);
  177. } else {
  178. $ret[$key] = $value;
  179. }
  180. }
  181. } else {
  182. $ret[] = $b;
  183. }
  184. return $ret;
  185. }