| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * array.php |
| 5 | * |
| 6 | * Copyright (c) 1999-2001 The Squirrelmail Development Team |
| 7 | * Licensed under the GNU GPL. For full terms see the file COPYING. |
| 8 | * |
| 9 | * This contains functions that work with array manipulation. They |
| 10 | * will help sort, and do other types of things with arrays |
| 11 | * |
| 12 | * $Id$ |
| 13 | */ |
| 14 | |
| 15 | |
| 16 | function ary_sort($ary,$col, $dir = 1){ |
| 17 | // The globals are used because USORT determines what is passed to comp2 |
| 18 | // These should be $this->col and $this->dir in a class |
| 19 | // Would beat using globals |
| 20 | if(!is_array($col)){ |
| 21 | $col = array($col); |
| 22 | } |
| 23 | $GLOBALS['col'] = $col; // Column or Columns as an array |
| 24 | if ($dir > 0) |
| 25 | $dir = 1; |
| 26 | else |
| 27 | $dir = -1; |
| 28 | $GLOBALS['dir'] = $dir; // Direction, a positive number for ascending a negative for descending |
| 29 | |
| 30 | usort($ary,'array_comp2'); |
| 31 | return $ary; |
| 32 | } |
| 33 | |
| 34 | function array_comp2($a,$b,$i = 0) { |
| 35 | global $col; |
| 36 | global $dir; |
| 37 | $c = count($col) -1; |
| 38 | if ($a[$col[$i]] == $b[$col[$i]]){ |
| 39 | $r = 0; |
| 40 | while($i < $c && $r == 0){ |
| 41 | $i++; |
| 42 | $r = comp2($a,$b,$i); |
| 43 | } |
| 44 | } elseif($a[$col[$i]] < $b[$col[$i]]){ |
| 45 | return (- $dir); |
| 46 | } |
| 47 | return $dir; |
| 48 | } |
| 49 | |
| 50 | function removeElement($array, $element) { |
| 51 | $j = 0; |
| 52 | for ($i = 0;$i < count($array);$i++) |
| 53 | if ($i != $element) { |
| 54 | $newArray[$j] = $array[$i]; |
| 55 | $j++; |
| 56 | } |
| 57 | |
| 58 | return $newArray; |
| 59 | } |
| 60 | |
| 61 | function array_cleave($array1, $column) |
| 62 | { |
| 63 | $key=0; |
| 64 | $array2 = array(); |
| 65 | while ($key < count($array1)) { |
| 66 | array_push($array2, $array1[$key][$column]); |
| 67 | $key++; |
| 68 | } |
| 69 | |
| 70 | return ($array2); |
| 71 | } |
| 72 | |
| 73 | ?> |