1--TEST-- 2Test ksort() function : usage variations - sort integer/float values 3--FILE-- 4<?php 5/* 6 * Testing ksort() by providing array of integer/float/mixed values for $array argument 7 * with following flag values: 8 * 1. flag value as default 9 * 2. SORT_REGULAR - compare items normally 10 * 3. SORT_NUMERIC - compare items numerically 11*/ 12 13echo "*** Testing ksort() : usage variations ***\n"; 14 15// diff. associative arrays to sort 16$various_arrays = array( 17 // negative/posative integer key value array 18 array(1 => 11, -2 => -11, 3 => 21, -4 => -21, 5 => 31, -6 => -31, 7 => 0, 8 => 41, -10 =>-41), 19 20 // mixed value array with different types of keys 21 array(1 => .0001, 2 => .0021, -3 => -.01, 4 => -1, 5 => 0, 6 => .09, 7 => 2, -8 => -.9, 22 9 => 10.6E-2, -10 => -10.6E-2, 11 => 33) 23); 24 25// set of possible flag values 26$flags = array("SORT_REGULAR" => SORT_REGULAR, "SORT_NUMERIC" => SORT_NUMERIC); 27 28$count = 1; 29echo "\n-- Testing ksort() by supplying various integer/float arrays --\n"; 30 31// loop through to test ksort() with different arrays 32foreach ($various_arrays as $array) { 33 echo "\n-- Iteration $count --\n"; 34 35 echo "- With default sort flag -\n"; 36 $temp_array = $array; 37 var_dump(ksort($temp_array) ); 38 var_dump($temp_array); 39 40 // loop through $flags array and call ksort() with all possible sort flag values 41 foreach($flags as $key => $flag){ 42 echo "- Sort flag = $key -\n"; 43 $temp_array = $array; 44 var_dump(ksort($temp_array, $flag) ); 45 var_dump($temp_array); 46 } 47 $count++; 48} 49 50echo "Done\n"; 51?> 52--EXPECT-- 53*** Testing ksort() : usage variations *** 54 55-- Testing ksort() by supplying various integer/float arrays -- 56 57-- Iteration 1 -- 58- With default sort flag - 59bool(true) 60array(9) { 61 [-10]=> 62 int(-41) 63 [-6]=> 64 int(-31) 65 [-4]=> 66 int(-21) 67 [-2]=> 68 int(-11) 69 [1]=> 70 int(11) 71 [3]=> 72 int(21) 73 [5]=> 74 int(31) 75 [7]=> 76 int(0) 77 [8]=> 78 int(41) 79} 80- Sort flag = SORT_REGULAR - 81bool(true) 82array(9) { 83 [-10]=> 84 int(-41) 85 [-6]=> 86 int(-31) 87 [-4]=> 88 int(-21) 89 [-2]=> 90 int(-11) 91 [1]=> 92 int(11) 93 [3]=> 94 int(21) 95 [5]=> 96 int(31) 97 [7]=> 98 int(0) 99 [8]=> 100 int(41) 101} 102- Sort flag = SORT_NUMERIC - 103bool(true) 104array(9) { 105 [-10]=> 106 int(-41) 107 [-6]=> 108 int(-31) 109 [-4]=> 110 int(-21) 111 [-2]=> 112 int(-11) 113 [1]=> 114 int(11) 115 [3]=> 116 int(21) 117 [5]=> 118 int(31) 119 [7]=> 120 int(0) 121 [8]=> 122 int(41) 123} 124 125-- Iteration 2 -- 126- With default sort flag - 127bool(true) 128array(11) { 129 [-10]=> 130 float(-0.106) 131 [-8]=> 132 float(-0.9) 133 [-3]=> 134 float(-0.01) 135 [1]=> 136 float(0.0001) 137 [2]=> 138 float(0.0021) 139 [4]=> 140 int(-1) 141 [5]=> 142 int(0) 143 [6]=> 144 float(0.09) 145 [7]=> 146 int(2) 147 [9]=> 148 float(0.106) 149 [11]=> 150 int(33) 151} 152- Sort flag = SORT_REGULAR - 153bool(true) 154array(11) { 155 [-10]=> 156 float(-0.106) 157 [-8]=> 158 float(-0.9) 159 [-3]=> 160 float(-0.01) 161 [1]=> 162 float(0.0001) 163 [2]=> 164 float(0.0021) 165 [4]=> 166 int(-1) 167 [5]=> 168 int(0) 169 [6]=> 170 float(0.09) 171 [7]=> 172 int(2) 173 [9]=> 174 float(0.106) 175 [11]=> 176 int(33) 177} 178- Sort flag = SORT_NUMERIC - 179bool(true) 180array(11) { 181 [-10]=> 182 float(-0.106) 183 [-8]=> 184 float(-0.9) 185 [-3]=> 186 float(-0.01) 187 [1]=> 188 float(0.0001) 189 [2]=> 190 float(0.0021) 191 [4]=> 192 int(-1) 193 [5]=> 194 int(0) 195 [6]=> 196 float(0.09) 197 [7]=> 198 int(2) 199 [9]=> 200 float(0.106) 201 [11]=> 202 int(33) 203} 204Done 205