array.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. function array_cleave($array1, $column)
  49. {
  50. $key=0;
  51. $array2 = array();
  52. while ($key < count($array1)) {
  53. array_push($array2, $array1[$key]["$column"]);
  54. $key++;
  55. }
  56. return ($array2);
  57. }
  58. ?>