array.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. <?
  2. /**
  3. ** array.php3
  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. function comp2($a,$b,$i = 0) {
  19. global $col;
  20. global $dir;
  21. $c = count($col) -1;
  22. if ($a["$col[$i]"] == $b["$col[$i]"]){
  23. $r = 0;
  24. while($i < $c && $r == 0){
  25. $i++;
  26. $r = comp2($a,$b,$i);
  27. }
  28. } elseif($a["$col[$i]"] < $b["$col[$i]"]){
  29. $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
  30. } else {
  31. $r = 1 * $dir;
  32. }
  33. return $r;
  34. }
  35. usort($ary,comp2);
  36. return $ary;
  37. }
  38. ?>