d17662bbdb22bba67612bf1abbe30a124b49dc8a
[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
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)){
17 $col = array($col);
18 }
19 $GLOBALS['col'] = $col; // Column or Columns as an array
20 if ($dir > 0)
21 $dir = 1;
22 else
23 $dir = -1;
24 $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending
25
26 usort($ary,'array_comp2');
27 return $ary;
28 }
29
30 function array_comp2($a,$b,$i = 0) {
31 global $col;
32 global $dir;
33 $c = count($col) -1;
34 if ($a[$col[$i]] == $b[$col[$i]]){
35 $r = 0;
36 while($i < $c && $r == 0){
37 $i++;
38 $r = comp2($a,$b,$i);
39 }
40 } elseif($a[$col[$i]] < $b[$col[$i]]){
41 return (- $dir);
42 }
43 return $dir;
44 }
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 }
56
57 function array_cleave($array1, $column)
58 {
59 $key=0;
60 $array2 = array();
61 while ($key < count($array1)) {
62 array_push($array2, $array1[$key][$column]);
63 $key++;
64 }
65
66 return ($array2);
67 }
68
69 ?>