array.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. $array_php = true;
  11. function ary_sort($ary,$col, $dir = 1){
  12. // The globals are used because USORT determines what is passed to comp2
  13. // These should be $this->col and $this->dir in a class
  14. // Would beat using globals
  15. if(!is_array($col)){
  16. $col = array("$col");
  17. }
  18. $GLOBALS["col"] = $col; // Column or Columns as an array
  19. $GLOBALS["dir"] = $dir; // Direction, a positive number for ascending a negative for descending
  20. usort($ary,'comp2');
  21. return $ary;
  22. }
  23. function comp2($a,$b,$i = 0) {
  24. global $col;
  25. global $dir;
  26. $c = count($col) -1;
  27. if ($a["$col[$i]"] == $b["$col[$i]"]){
  28. $r = 0;
  29. while($i < $c && $r == 0){
  30. $i++;
  31. $r = comp2($a,$b,$i);
  32. }
  33. } elseif($a["$col[$i]"] < $b["$col[$i]"]){
  34. $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
  35. } else {
  36. $r = 1 * $dir;
  37. }
  38. return $r;
  39. }
  40. function removeElement($array, $element) {
  41. $j = 0;
  42. for ($i = 0;$i < count($array);$i++)
  43. if ($i != $element) {
  44. $newArray[$j] = $array[$i];
  45. $j++;
  46. }
  47. return $newArray;
  48. }
  49. function array_cleave($array1, $column)
  50. {
  51. $key=0;
  52. $array2 = array();
  53. while ($key < count($array1)) {
  54. array_push($array2, $array1[$key]["$column"]);
  55. $key++;
  56. }
  57. return ($array2);
  58. }
  59. ?>