array.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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. if ($dir > 0)
  20. $dir = 1;
  21. else
  22. $dir = -1;
  23. $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending
  24. usort($ary,'array_comp2');
  25. return $ary;
  26. }
  27. function array_comp2($a,$b,$i = 0) {
  28. global $col;
  29. global $dir;
  30. $c = count($col) -1;
  31. if ($a[$col[$i]] == $b[$col[$i]]){
  32. $r = 0;
  33. while($i < $c && $r == 0){
  34. $i++;
  35. $r = comp2($a,$b,$i);
  36. }
  37. } elseif($a[$col[$i]] < $b[$col[$i]]){
  38. return (- $dir);
  39. }
  40. return $dir;
  41. }
  42. function removeElement($array, $element) {
  43. $j = 0;
  44. for ($i = 0;$i < count($array);$i++)
  45. if ($i != $element) {
  46. $newArray[$j] = $array[$i];
  47. $j++;
  48. }
  49. return $newArray;
  50. }
  51. function array_cleave($array1, $column)
  52. {
  53. $key=0;
  54. $array2 = array();
  55. while ($key < count($array1)) {
  56. array_push($array2, $array1[$key][$column]);
  57. $key++;
  58. }
  59. return ($array2);
  60. }
  61. ?>