array.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. **/
  9. $array_php = true;
  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. $GLOBALS["dir"] = $dir; // Direction, a positive number for ascending a negative for descending
  19. usort($ary,comp2);
  20. return $ary;
  21. }
  22. function comp2($a,$b,$i = 0) {
  23. global $col;
  24. global $dir;
  25. $c = count($col) -1;
  26. if ($a["$col[$i]"] == $b["$col[$i]"]){
  27. $r = 0;
  28. while($i < $c && $r == 0){
  29. $i++;
  30. $r = comp2($a,$b,$i);
  31. }
  32. } elseif($a["$col[$i]"] < $b["$col[$i]"]){
  33. $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
  34. } else {
  35. $r = 1 * $dir;
  36. }
  37. return $r;
  38. }
  39. function removeElement($array, $element) {
  40. $j = 0;
  41. for ($i = 0;$i < count($array);$i++)
  42. if ($i != $element) {
  43. $newArray[$j] = $array[$i];
  44. $j++;
  45. }
  46. return $newArray;
  47. }
  48. ?>