array.php 1.6 KB

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