array.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. // The globals are used because USORT determines what is passed to comp2
  15. // These should be $this->col and $this->dir in a class
  16. // Would beat using globals
  17. if(!is_array($col)){
  18. $col = array($col);
  19. }
  20. $GLOBALS['col'] = $col; // Column or Columns as an array
  21. if ($dir > 0)
  22. $dir = 1;
  23. else
  24. $dir = -1;
  25. $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending
  26. usort($ary,'array_comp2');
  27. return $ary;
  28. }
  29. function array_comp2($a,$b,$i = 0) {
  30. global $col;
  31. global $dir;
  32. $c = count($col) -1;
  33. if ($a[$col[$i]] == $b[$col[$i]]){
  34. $r = 0;
  35. while($i < $c && $r == 0){
  36. $i++;
  37. $r = comp2($a,$b,$i);
  38. }
  39. } elseif($a[$col[$i]] < $b[$col[$i]]){
  40. return (- $dir);
  41. }
  42. return $dir;
  43. }
  44. function removeElement($array, $element) {
  45. $j = 0;
  46. for ($i = 0;$i < count($array);$i++)
  47. if ($i != $element) {
  48. $newArray[$j] = $array[$i];
  49. $j++;
  50. }
  51. return $newArray;
  52. }
  53. function array_cleave($array1, $column)
  54. {
  55. $key=0;
  56. $array2 = array();
  57. while ($key < count($array1)) {
  58. array_push($array2, $array1[$key][$column]);
  59. $key++;
  60. }
  61. return ($array2);
  62. }
  63. ?>