fix bug #481886 with anonymous hint from sourceforge trackers :-)
[squirrelmail.git] / functions / array.php
CommitLineData
59177427 1<?php
3302d0d4 2 /**
23d6bd09 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 */
f435778e 10
d068c0ec 11
3302d0d4 12 function ary_sort($ary,$col, $dir = 1){
13 // The globals are used because USORT determines what is passed to comp2
14 // These should be $this->col and $this->dir in a class
15 // Would beat using globals
16 if(!is_array($col)){
b9bfd165 17 $col = array($col);
3302d0d4 18 }
b9bfd165 19 $GLOBALS['col'] = $col; // Column or Columns as an array
485599c7 20 if ($dir > 0)
21 $dir = 1;
22 else
23 $dir = -1;
b9bfd165 24 $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending
7ce342dc 25
485599c7 26 usort($ary,'array_comp2');
7ce342dc 27 return $ary;
28 }
29
485599c7 30 function array_comp2($a,$b,$i = 0) {
3302d0d4 31 global $col;
32 global $dir;
33 $c = count($col) -1;
b9bfd165 34 if ($a[$col[$i]] == $b[$col[$i]]){
3302d0d4 35 $r = 0;
36 while($i < $c && $r == 0){
37 $i++;
38 $r = comp2($a,$b,$i);
39 }
b9bfd165 40 } elseif($a[$col[$i]] < $b[$col[$i]]){
485599c7 41 return (- $dir);
42 }
43 return $dir;
3302d0d4 44 }
7cad6205 45
46 function removeElement($array, $element) {
47 $j = 0;
48 for ($i = 0;$i < count($array);$i++)
49 if ($i != $element) {
50 $newArray[$j] = $array[$i];
51 $j++;
52 }
53
54 return $newArray;
55 }
5b6ae78a 56
57 function array_cleave($array1, $column)
58 {
59 $key=0;
60 $array2 = array();
61 while ($key < count($array1)) {
b9bfd165 62 array_push($array2, $array1[$key][$column]);
5b6ae78a 63 $key++;
64 }
65
66 return ($array2);
67 }
68
3302d0d4 69?>