array.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. function removeElement($array, $element) {
  39. $j = 0;
  40. for ($i = 0;$i < count($array);$i++)
  41. if ($i != $element) {
  42. $newArray[$j] = $array[$i];
  43. $j++;
  44. }
  45. return $newArray;
  46. }
  47. ?>