array.lib.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 a dimensional array
  13. * @return array an array with unique values
  14. *
  15. */
  16. function array_unique_dimensional($array) {
  17. if(!is_array($array))
  18. return $array;
  19. foreach ($array as &$myvalue) {
  20. $myvalue=serialize($myvalue);
  21. }
  22. $array=array_unique($array);
  23. foreach ($array as &$myvalue) {
  24. $myvalue=unserialize($myvalue);
  25. }
  26. return $array;
  27. }
  28. /**
  29. *
  30. * Sort multidimensional arrays
  31. *
  32. * @param array unsorted multidimensional array
  33. * @param string key to be sorted
  34. * @return array result array
  35. * @author found in http://php.net/manual/en/function.sort.php
  36. */
  37. function msort($array, $id='id', $order = 'desc') {
  38. if (empty($array)) {
  39. return $array;
  40. }
  41. $temp_array = array();
  42. while (count($array)>0) {
  43. $lowest_id = 0;
  44. $index=0;
  45. foreach ($array as $item) {
  46. if ($order == 'desc') {
  47. if ($item[$id]<$array[$lowest_id][$id]) {
  48. $lowest_id = $index;
  49. }
  50. } else {
  51. if ($item[$id]>$array[$lowest_id][$id]) {
  52. $lowest_id = $index;
  53. }
  54. }
  55. $index++;
  56. }
  57. $temp_array[] = $array[$lowest_id];
  58. $array = array_merge(array_slice($array, 0, $lowest_id), array_slice($array, $lowest_id+1));
  59. }
  60. return $temp_array;
  61. }