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