b59f21c960e76612ae1fa8cb32c3881319c32bb2
[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 $GLOBALS["dir"] = $dir; // Direction, a positive number for ascending a negative for descending
22
23 usort($ary,'comp2');
24 return $ary;
25 }
26
27 function 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 $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
39 } else {
40 $r = 1 * $dir;
41 }
42 return $r;
43 }
44
45 function removeElement($array, $element) {
46 $j = 0;
47 for ($i = 0;$i < count($array);$i++)
48 if ($i != $element) {
49 $newArray[$j] = $array[$i];
50 $j++;
51 }
52
53 return $newArray;
54 }
55
56 function array_cleave($array1, $column)
57 {
58 $key=0;
59 $array2 = array();
60 while ($key < count($array1)) {
61 array_push($array2, $array1[$key]["$column"]);
62 $key++;
63 }
64
65 return ($array2);
66 }
67
68 ?>