Applied patch #102655. Ha! my first commit !
[squirrelmail.git] / functions / array.php
CommitLineData
59177427 1<?php
3302d0d4 2 /**
a09387f4 3 ** array.php
3302d0d4 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 **/
9
d068c0ec 10 $array_php = true;
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)){
17 $col = array("$col");
18 }
19 $GLOBALS["col"] = $col; // Column or Columns as an array
20 $GLOBALS["dir"] = $dir; // Direction, a positive number for ascending a negative for descending
7ce342dc 21
22 usort($ary,comp2);
23 return $ary;
24 }
25
26 function comp2($a,$b,$i = 0) {
3302d0d4 27 global $col;
28 global $dir;
29 $c = count($col) -1;
30 if ($a["$col[$i]"] == $b["$col[$i]"]){
31 $r = 0;
32 while($i < $c && $r == 0){
33 $i++;
34 $r = comp2($a,$b,$i);
35 }
36 } elseif($a["$col[$i]"] < $b["$col[$i]"]){
37 $r = -1 * $dir; // Im not sure why you must * dir here, but it wont work just before the return...
38 } else {
39 $r = 1 * $dir;
40 }
41 return $r;
42 }
7cad6205 43
44 function removeElement($array, $element) {
45 $j = 0;
46 for ($i = 0;$i < count($array);$i++)
47 if ($i != $element) {
48 $newArray[$j] = $array[$i];
49 $j++;
50 }
51
52 return $newArray;
53 }
5b6ae78a 54
55 function array_cleave($array1, $column)
56 {
57 $key=0;
58 $array2 = array();
59 while ($key < count($array1)) {
267abcbe 60 array_push($array2, $array1[$key]["$column"]);
5b6ae78a 61 $key++;
62 }
63
64 return ($array2);
65 }
66
3302d0d4 67?>