array.lib.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * This is the array library for Chamilo.
  5. * Include/require it in your code to use its functionality.
  6. *
  7. * @package chamilo.library
  8. */
  9. class ArrayClass
  10. {
  11. /**
  12. * Removes duplicate values from a dimensional array
  13. *
  14. * @param array a dimensional array
  15. * @return array an array with unique values
  16. *
  17. */
  18. public static function array_unique_dimensional($array)
  19. {
  20. if (!is_array($array)) {
  21. return $array;
  22. }
  23. foreach ($array as &$myvalue) {
  24. $myvalue = serialize($myvalue);
  25. }
  26. $array = array_unique($array);
  27. foreach ($array as &$myvalue) {
  28. $myvalue = unserialize($myvalue);
  29. }
  30. return $array;
  31. }
  32. /**
  33. *
  34. * Sort multidimensional arrays
  35. *
  36. * @param array unsorted multidimensional array
  37. * @param string key to be sorted
  38. * @return array result array
  39. * @author found in http://php.net/manual/en/function.sort.php
  40. */
  41. public static function msort($array, $id = 'id', $order = 'desc')
  42. {
  43. if (empty($array)) {
  44. return $array;
  45. }
  46. $temp_array = array();
  47. while (count($array) > 0) {
  48. $lowest_id = 0;
  49. $index = 0;
  50. foreach ($array as $item) {
  51. if ($order == 'desc') {
  52. if ($item[$id] < $array[$lowest_id][$id]) {
  53. $lowest_id = $index;
  54. }
  55. } else {
  56. if ($item[$id] > $array[$lowest_id][$id]) {
  57. $lowest_id = $index;
  58. }
  59. }
  60. $index++;
  61. }
  62. $temp_array[] = $array[$lowest_id];
  63. $array = array_merge(
  64. array_slice($array, 0, $lowest_id),
  65. array_slice($array, $lowest_id + 1)
  66. );
  67. }
  68. return $temp_array;
  69. }
  70. /**
  71. * @param $array
  72. * @return mixed
  73. */
  74. public static function utf8_sort($array)
  75. {
  76. $old_locale = setlocale(LC_ALL, null);
  77. $code = api_get_language_isocode();
  78. $locale_list = array(
  79. $code.'.utf8',
  80. 'en.utf8',
  81. 'en_US.utf8',
  82. 'en_GB.utf8',
  83. );
  84. $try_sort = false;
  85. foreach ($locale_list as $locale) {
  86. $my_local = setlocale(LC_COLLATE, $locale);
  87. if ($my_local) {
  88. $try_sort = true;
  89. break;
  90. }
  91. }
  92. if ($try_sort) {
  93. uasort($array, 'strcoll');
  94. }
  95. setlocale(LC_COLLATE, $old_locale);
  96. return $array;
  97. }
  98. /**
  99. * @param $array
  100. * @param string $separator
  101. * @return string
  102. */
  103. public static function array_to_string($array, $separator = ',')
  104. {
  105. if (empty($array)) {
  106. return '';
  107. }
  108. return implode($separator.' ', $array);
  109. }
  110. /**
  111. * @param array $array
  112. * @return array
  113. */
  114. public static function array_flatten(array $array)
  115. {
  116. $flatten = array();
  117. array_walk_recursive(
  118. $array,
  119. function ($value) use (&$flatten) {
  120. $flatten[] = $value;
  121. }
  122. );
  123. return $flatten;
  124. }
  125. /**
  126. * Shuffles an array keeping the associations
  127. * @param $array
  128. * @return bool
  129. */
  130. public static function shuffle_assoc(&$array)
  131. {
  132. $keys = array_keys($array);
  133. shuffle($keys);
  134. foreach ($keys as $key) {
  135. $new[$key] = $array[$key];
  136. }
  137. $array = $new;
  138. return true;
  139. }
  140. public static function array_walk_recursive_limited(
  141. &$array,
  142. $function,
  143. $apply_to_keys_also = false
  144. ) {
  145. static $recursive_counter = 0;
  146. if (++$recursive_counter > 1000) {
  147. die('possible deep recursion attack');
  148. }
  149. foreach ($array as $key => $value) {
  150. if (is_array($value)) {
  151. array_walk_recursive_limited(
  152. $array[$key],
  153. $function,
  154. $apply_to_keys_also
  155. );
  156. } else {
  157. $array[$key] = $function($value);
  158. }
  159. if ($apply_to_keys_also && is_string($key)) {
  160. $new_key = $function($key);
  161. if ($new_key != $key) {
  162. $array[$new_key] = $array[$key];
  163. unset($array[$key]);
  164. }
  165. }
  166. }
  167. $recursive_counter--;
  168. }
  169. }
  170. /**
  171. * @param array $array
  172. * @return array
  173. */
  174. function array_flatten(array $array)
  175. {
  176. $flatten = array();
  177. array_walk_recursive(
  178. $array,
  179. function ($value) use (&$flatten) {
  180. $flatten[] = $value;
  181. }
  182. );
  183. return $flatten;
  184. }
  185. /**
  186. * Shuffles an array keeping the associations
  187. * @param $array
  188. * @return bool
  189. */
  190. function shuffle_assoc(&$array)
  191. {
  192. $keys = array_keys($array);
  193. shuffle($keys);
  194. $new = array();
  195. foreach ($keys as $key) {
  196. $new[$key] = $array[$key];
  197. }
  198. $array = $new;
  199. return true;
  200. }