array.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. /**
  3. * array.php
  4. *
  5. * Copyright (c) 1999-2001 The Squirrelmail Development Team
  6. * Licensed under the GNU GPL. For full terms see the file COPYING.
  7. *
  8. * This contains functions that work with array manipulation. They
  9. * will help sort, and do other types of things with arrays
  10. *
  11. * $Id$
  12. */
  13. /*****************************************************************/
  14. /*** THIS FILE NEEDS TO HAVE ITS FORMATTING FIXED!!! ***/
  15. /*** PLEASE DO SO AND REMOVE THIS COMMENT SECTION. ***/
  16. /*** + Base level indent should begin at left margin, as ***/
  17. /*** the first line of the function definition below. ***/
  18. /*** + All identation should consist of four space blocks ***/
  19. /*** + Tab characters are evil. ***/
  20. /*** + all comments should use "slash-star ... star-slash" ***/
  21. /*** style -- no pound characters, no slash-slash style ***/
  22. /*** + FLOW CONTROL STATEMENTS (if, while, etc) SHOULD ***/
  23. /*** ALWAYS USE { AND } CHARACTERS!!! ***/
  24. /*** + Please use ' instead of ", when possible. Note " ***/
  25. /*** should always be used in _( ) function calls. ***/
  26. /*** Thank you for your help making the SM code more readable. ***/
  27. /*****************************************************************/
  28. function ary_sort($ary,$col, $dir = 1){
  29. // The globals are used because USORT determines what is passed to comp2
  30. // These should be $this->col and $this->dir in a class
  31. // Would beat using globals
  32. if(!is_array($col)){
  33. $col = array($col);
  34. }
  35. $GLOBALS['col'] = $col; // Column or Columns as an array
  36. if ($dir > 0)
  37. $dir = 1;
  38. else
  39. $dir = -1;
  40. $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending
  41. usort($ary,'array_comp2');
  42. return $ary;
  43. }
  44. function array_comp2($a,$b,$i = 0) {
  45. global $col;
  46. global $dir;
  47. $c = count($col) -1;
  48. if ($a[$col[$i]] == $b[$col[$i]]){
  49. $r = 0;
  50. while($i < $c && $r == 0){
  51. $i++;
  52. $r = comp2($a,$b,$i);
  53. }
  54. } elseif($a[$col[$i]] < $b[$col[$i]]){
  55. return (- $dir);
  56. }
  57. return $dir;
  58. }
  59. function removeElement($array, $element) {
  60. $j = 0;
  61. for ($i = 0;$i < count($array);$i++)
  62. if ($i != $element) {
  63. $newArray[$j] = $array[$i];
  64. $j++;
  65. }
  66. return $newArray;
  67. }
  68. function array_cleave($array1, $column)
  69. {
  70. $key=0;
  71. $array2 = array();
  72. while ($key < count($array1)) {
  73. array_push($array2, $array1[$key][$column]);
  74. $key++;
  75. }
  76. return ($array2);
  77. }
  78. ?>