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