array.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?
  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. **/
  9. function ary_sort($ary,$col, $dir = 1){
  10. // The globals are used because USORT determines what is passed to comp2
  11. // These should be $this->col and $this->dir in a class
  12. // Would beat using globals
  13. if(!is_array($col)){
  14. $col = array("$col");
  15. }
  16. $GLOBALS["col"] = $col; // Column or Columns as an array
  17. $GLOBALS["dir"] = $dir; // Direction, a positive number for ascending a negative for descending
  18. usort($ary,comp2);
  19. return $ary;
  20. }
  21. function comp2($a,$b,$i = 0) {
  22. global $col;
  23. global $dir;
  24. $c = count($col) -1;
  25. if ($a["$col[$i]"] == $b["$col[$i]"]){
  26. $r = 0;
  27. while($i < $c && $r == 0){
  28. $i++;
  29. $r = comp2($a,$b,$i);
  30. }
  31. } elseif($a["$col[$i]"] < $b["$col[$i]"]){
  32. $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
  33. } else {
  34. $r = 1 * $dir;
  35. }
  36. return $r;
  37. }
  38. ?>