array.lib.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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(array_slice($array, 0, $lowest_id), array_slice($array, $lowest_id + 1));
  64. }
  65. return $temp_array;
  66. }
  67. /**
  68. * @param $array
  69. * @return mixed
  70. */
  71. public static function utf8_sort($array)
  72. {
  73. $old_locale = setlocale(LC_ALL, null);
  74. $code = api_get_language_isocode();
  75. $locale_list = array($code.'.utf8', 'en.utf8', 'en_US.utf8', 'en_GB.utf8');
  76. $try_sort = false;
  77. foreach ($locale_list as $locale) {
  78. $my_local = setlocale(LC_COLLATE, $locale);
  79. if ($my_local) {
  80. $try_sort = true;
  81. break;
  82. }
  83. }
  84. if ($try_sort) {
  85. uasort($array, 'strcoll');
  86. }
  87. setlocale(LC_COLLATE, $old_locale);
  88. return $array;
  89. }
  90. /**
  91. * @param $array
  92. * @param string $separator
  93. * @return string
  94. */
  95. public static function array_to_string($array, $separator = ',')
  96. {
  97. if (empty($array)) {
  98. return '';
  99. }
  100. return implode($separator.' ', $array);
  101. }
  102. /**
  103. * @param array $array
  104. * @return array
  105. */
  106. public static function array_flatten(array $array)
  107. {
  108. $flatten = array();
  109. array_walk_recursive(
  110. $array,
  111. function ($value) use (&$flatten) {
  112. $flatten[] = $value;
  113. }
  114. );
  115. return $flatten;
  116. }
  117. /**
  118. * Shuffles an array keeping the associations
  119. * @param $array
  120. * @return bool
  121. */
  122. public static function shuffle_assoc(&$array)
  123. {
  124. $keys = array_keys($array);
  125. shuffle($keys);
  126. foreach ($keys as $key) {
  127. $new[$key] = $array[$key];
  128. }
  129. $array = $new;
  130. return true;
  131. }
  132. }