array.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * array.php
  4. *
  5. * Copyright (c) 1999-2001 The Squirrelmail Development Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains functions that work with array manipulation. They
  9. * will help sort, and do other types of things with arrays
  10. *
  11. * $Id$
  12. */
  13. function ary_sort($ary,$col, $dir = 1)
  14. {
  15. /* The globals are used because USORT determines what is passed to comp2 */
  16. /* These should be $this->col and $this->dir in a class */
  17. /* Would beat using globals */
  18. if (!is_array($col)) {
  19. $col = array($col);
  20. }
  21. $GLOBALS['col'] = $col; /* Column or Columns as an array */
  22. if ($dir > 0) {
  23. $dir = 1;
  24. }
  25. else {
  26. $dir = -1;
  27. }
  28. /* Direction, a positive number for ascending a negative for descending */
  29. $GLOBALS['dir'] = $dir;
  30. usort($ary,'array_comp2');
  31. return $ary;
  32. }
  33. function array_comp2($a,$b,$i = 0)
  34. {
  35. global $col;
  36. global $dir;
  37. $c = count($col) -1;
  38. if ($a[$col[$i]] == $b[$col[$i]]) {
  39. $r = 0;
  40. while ($i < $c && $r == 0) {
  41. $i++;
  42. $r = comp2($a,$b,$i);
  43. }
  44. }
  45. elseif ($a[$col[$i]] < $b[$col[$i]]) {
  46. return (- $dir);
  47. }
  48. return $dir;
  49. }
  50. function removeElement($array, $element)
  51. {
  52. $j = 0;
  53. for ($i = 0;$i < count($array);$i++) {
  54. if ($i != $element) {
  55. $newArray[$j] = $array[$i];
  56. $j++;
  57. }
  58. }
  59. return $newArray;
  60. }
  61. function array_cleave($array1, $column)
  62. {
  63. $key=0;
  64. $array2 = array();
  65. while ($key < count($array1)) {
  66. array_push($array2, $array1[$key][$column]);
  67. $key++;
  68. }
  69. return ($array2);
  70. }
  71. ?>