array.lib.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. * @return array an array with unique values
  14. *
  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 = unserialize($myvalue);
  27. }
  28. return $array;
  29. }
  30. /**
  31. *
  32. * Sort multidimensional arrays
  33. *
  34. * @param array unsorted multidimensional array
  35. * @param string key to be sorted
  36. * @return array result array
  37. * @author found in http://php.net/manual/en/function.sort.php
  38. */
  39. function msort($array, $id = 'id', $order = 'desc')
  40. {
  41. if (empty($array)) {
  42. return $array;
  43. }
  44. $temp_array = array();
  45. while (count($array) > 0) {
  46. $lowest_id = 0;
  47. $index = 0;
  48. foreach ($array as $item) {
  49. if ($order == 'desc') {
  50. if ($item[$id] < $array[$lowest_id][$id]) {
  51. $lowest_id = $index;
  52. }
  53. } else {
  54. if ($item[$id] > $array[$lowest_id][$id]) {
  55. $lowest_id = $index;
  56. }
  57. }
  58. $index++;
  59. }
  60. $temp_array[] = $array[$lowest_id];
  61. $array = array_merge(
  62. array_slice($array, 0, $lowest_id),
  63. array_slice($array, $lowest_id + 1)
  64. );
  65. }
  66. return $temp_array;
  67. }
  68. function array_walk_recursive_limited(
  69. &$array,
  70. $function,
  71. $apply_to_keys_also = false
  72. ) {
  73. static $recursive_counter = 0;
  74. if (++$recursive_counter > 1000) {
  75. die('possible deep recursion attack');
  76. }
  77. foreach ($array as $key => $value) {
  78. if (is_array($value)) {
  79. array_walk_recursive_limited(
  80. $array[$key],
  81. $function,
  82. $apply_to_keys_also
  83. );
  84. } else {
  85. $array[$key] = $function($value);
  86. }
  87. if ($apply_to_keys_also && is_string($key)) {
  88. $new_key = $function($key);
  89. if ($new_key != $key) {
  90. $array[$new_key] = $array[$key];
  91. unset($array[$key]);
  92. }
  93. }
  94. }
  95. $recursive_counter--;
  96. }
  97. /**
  98. * @param $array
  99. * @return mixed
  100. */
  101. function utf8_sort($array)
  102. {
  103. $old_locale = setlocale(LC_ALL, null);
  104. $code = api_get_language_isocode();
  105. $locale_list = array($code.'.utf8', 'en.utf8','en_US.utf8','en_GB.utf8');
  106. $try_sort = false;
  107. foreach($locale_list as $locale) {
  108. $my_local = setlocale(LC_COLLATE, $locale);
  109. if ($my_local) {
  110. $try_sort = true;
  111. break;
  112. }
  113. }
  114. if ($try_sort) {
  115. uasort($array, 'strcoll');
  116. }
  117. setlocale(LC_COLLATE, $old_locale);
  118. return $array;
  119. }
  120. /**
  121. * @param array $array
  122. * @param string $separator
  123. * @return string
  124. */
  125. function array_to_string($array, $separator = ',')
  126. {
  127. if (empty($array)) {
  128. return '';
  129. }
  130. return implode($separator.' ', $array);
  131. }