array.lib.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. /**
  10. * Removes duplicate values from a dimensional array.
  11. *
  12. * @param array $array dimensional array
  13. *
  14. * @return array an array with unique values
  15. */
  16. function array_unique_dimensional($array)
  17. {
  18. if (!is_array($array)) {
  19. return $array;
  20. }
  21. foreach ($array as &$myvalue) {
  22. $myvalue = serialize($myvalue);
  23. }
  24. $array = array_unique($array);
  25. foreach ($array as &$myvalue) {
  26. $myvalue = UnserializeApi::unserialize('not_allowed_clases', $myvalue);
  27. }
  28. return $array;
  29. }
  30. /**
  31. * Sort multidimensional arrays.
  32. *
  33. * @param array unsorted multidimensional array
  34. * @param string key to be sorted
  35. *
  36. * @return array result array
  37. *
  38. * @author found in http://php.net/manual/en/function.sort.php
  39. */
  40. function msort($array, $id = 'id', $order = 'desc')
  41. {
  42. if (empty($array)) {
  43. return $array;
  44. }
  45. $temp_array = [];
  46. while (count($array) > 0) {
  47. $lowest_id = 0;
  48. $index = 0;
  49. foreach ($array as $item) {
  50. if ($order == 'desc') {
  51. if ($item[$id] < $array[$lowest_id][$id]) {
  52. $lowest_id = $index;
  53. }
  54. } else {
  55. if (isset($item[$id]) && $item[$id] > $array[$lowest_id][$id]) {
  56. $lowest_id = $index;
  57. }
  58. }
  59. $index++;
  60. }
  61. $temp_array[] = $array[$lowest_id];
  62. $array = array_merge(
  63. array_slice($array, 0, $lowest_id),
  64. array_slice($array, $lowest_id + 1)
  65. );
  66. }
  67. return $temp_array;
  68. }
  69. /**
  70. * @param $array
  71. *
  72. * @return mixed
  73. */
  74. function utf8_sort($array)
  75. {
  76. $old_locale = setlocale(LC_ALL, 0);
  77. $code = api_get_language_isocode();
  78. $locale_list = [$code.'.utf8', 'en.utf8', 'en_US.utf8', 'en_GB.utf8'];
  79. $try_sort = false;
  80. foreach ($locale_list as $locale) {
  81. $my_local = setlocale(LC_COLLATE, $locale);
  82. if ($my_local) {
  83. $try_sort = true;
  84. break;
  85. }
  86. }
  87. if ($try_sort) {
  88. uasort($array, 'strcoll');
  89. }
  90. setlocale(LC_COLLATE, $old_locale);
  91. return $array;
  92. }
  93. /**
  94. * @param array $array
  95. * @param string $separator
  96. *
  97. * @return string
  98. */
  99. function array_to_string($array, $separator = ',')
  100. {
  101. if (empty($array)) {
  102. return '';
  103. }
  104. return implode($separator.' ', $array);
  105. }
  106. /**
  107. * @param array $array
  108. *
  109. * @return array
  110. */
  111. function array_flatten(array $array)
  112. {
  113. $flatten = [];
  114. array_walk_recursive(
  115. $array,
  116. function ($value) use (&$flatten) {
  117. $flatten[] = $value;
  118. }
  119. );
  120. return $flatten;
  121. }
  122. /**
  123. * Shuffles an array keeping the associations.
  124. *
  125. * @param $array
  126. *
  127. * @return bool
  128. */
  129. function shuffle_assoc(&$array)
  130. {
  131. $keys = array_keys($array);
  132. shuffle($keys);
  133. $new = [];
  134. foreach ($keys as $key) {
  135. $new[$key] = $array[$key];
  136. }
  137. $array = $new;
  138. return true;
  139. }