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 | ** |
245a6892 |
8 | ** $Id$ |
3302d0d4 |
9 | **/ |
10 | |
d068c0ec |
11 | $array_php = true; |
12 | |
3302d0d4 |
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 |
7ce342dc |
22 | |
245a6892 |
23 | usort($ary,'comp2'); |
7ce342dc |
24 | return $ary; |
25 | } |
26 | |
27 | function comp2($a,$b,$i = 0) { |
3302d0d4 |
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 | } |
7cad6205 |
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 | } |
5b6ae78a |
55 | |
56 | function array_cleave($array1, $column) |
57 | { |
58 | $key=0; |
59 | $array2 = array(); |
60 | while ($key < count($array1)) { |
267abcbe |
61 | array_push($array2, $array1[$key]["$column"]); |
5b6ae78a |
62 | $key++; |
63 | } |
64 | |
65 | return ($array2); |
66 | } |
67 | |
3302d0d4 |
68 | ?> |