1--TEST-- 2Test ksort() function : basic functionality 3--FILE-- 4<?php 5/* 6 * Testing ksort() by providing array of integer/string values to check the basic functionality with following flag values : 7 * 1.flag value as default 8 * 2.SORT_REGULAR - compare items normally 9 * 3.SORT_NUMERIC - compare items numerically 10 * 4.SORT_STRING - compare items as strings 11*/ 12 13echo "*** Testing ksort() : basic functionality ***\n"; 14 15// an array containing unsorted string values with indices 16$unsorted_strings = array( 17 "l" => "lemon", "o" => "orange", 18 "O" => "Orange", "O1" => "Orange1", "o2" => "orange2", "O3" => "Orange3", "o20" => "orange20", 19 "b" => "banana", 20); 21// an array containing unsorted numeric values with indices 22$unsorted_numerics = array( 100 => 4, 33 => 3, 555 => 2, 22 => 1 ); 23 24echo "\n-- Testing ksort() by supplying string array, 'flag' value is default --\n"; 25$temp_array = $unsorted_strings; 26var_dump( ksort($temp_array) ); // expecting : bool(true) 27var_dump( $temp_array); 28 29echo "\n-- Testing ksort() by supplying numeric array, 'flag' value is default --\n"; 30$temp_array = $unsorted_numerics; 31var_dump( ksort($temp_array) ); // expecting : bool(true) 32var_dump( $temp_array); 33 34echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_REGULAR --\n"; 35$temp_array = $unsorted_strings; 36var_dump( ksort($temp_array, SORT_REGULAR) ); // expecting : bool(true) 37var_dump( $temp_array); 38 39echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_REGULAR --\n"; 40$temp_array = $unsorted_numerics; 41var_dump( ksort($temp_array, SORT_REGULAR) ); // expecting : bool(true) 42var_dump( $temp_array); 43 44echo "\n-- Testing ksort() by supplying string array, 'flag' = SORT_STRING --\n"; 45$temp_array = $unsorted_strings; 46var_dump( ksort($temp_array, SORT_STRING) ); // expecting : bool(true) 47var_dump( $temp_array); 48 49echo "\n-- Testing ksort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE --\n"; 50$temp_array = $unsorted_strings; 51var_dump( ksort($temp_array, SORT_STRING|SORT_FLAG_CASE) ); // expecting : bool(true) 52var_dump( $temp_array); 53 54echo "\n-- Testing ksort() by supplying string array (natural), 'flag' = SORT_NATURAL --\n"; 55$temp_array = $unsorted_strings; 56var_dump( ksort($temp_array, SORT_NATURAL) ); // expecting : bool(true) 57var_dump( $temp_array); 58 59echo "\n-- Testing ksort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE --\n"; 60$temp_array = $unsorted_strings; 61var_dump( ksort($temp_array, SORT_NATURAL|SORT_FLAG_CASE) ); // expecting : bool(true) 62var_dump( $temp_array); 63 64echo "\n-- Testing ksort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n"; 65$temp_array = $unsorted_numerics; 66var_dump( ksort($temp_array, SORT_NUMERIC) ); // expecting : bool(true) 67var_dump( $temp_array); 68 69echo "Done\n"; 70?> 71--EXPECT-- 72*** Testing ksort() : basic functionality *** 73 74-- Testing ksort() by supplying string array, 'flag' value is default -- 75bool(true) 76array(8) { 77 ["O"]=> 78 string(6) "Orange" 79 ["O1"]=> 80 string(7) "Orange1" 81 ["O3"]=> 82 string(7) "Orange3" 83 ["b"]=> 84 string(6) "banana" 85 ["l"]=> 86 string(5) "lemon" 87 ["o"]=> 88 string(6) "orange" 89 ["o2"]=> 90 string(7) "orange2" 91 ["o20"]=> 92 string(8) "orange20" 93} 94 95-- Testing ksort() by supplying numeric array, 'flag' value is default -- 96bool(true) 97array(4) { 98 [22]=> 99 int(1) 100 [33]=> 101 int(3) 102 [100]=> 103 int(4) 104 [555]=> 105 int(2) 106} 107 108-- Testing ksort() by supplying string array, 'flag' = SORT_REGULAR -- 109bool(true) 110array(8) { 111 ["O"]=> 112 string(6) "Orange" 113 ["O1"]=> 114 string(7) "Orange1" 115 ["O3"]=> 116 string(7) "Orange3" 117 ["b"]=> 118 string(6) "banana" 119 ["l"]=> 120 string(5) "lemon" 121 ["o"]=> 122 string(6) "orange" 123 ["o2"]=> 124 string(7) "orange2" 125 ["o20"]=> 126 string(8) "orange20" 127} 128 129-- Testing ksort() by supplying numeric array, 'flag' = SORT_REGULAR -- 130bool(true) 131array(4) { 132 [22]=> 133 int(1) 134 [33]=> 135 int(3) 136 [100]=> 137 int(4) 138 [555]=> 139 int(2) 140} 141 142-- Testing ksort() by supplying string array, 'flag' = SORT_STRING -- 143bool(true) 144array(8) { 145 ["O"]=> 146 string(6) "Orange" 147 ["O1"]=> 148 string(7) "Orange1" 149 ["O3"]=> 150 string(7) "Orange3" 151 ["b"]=> 152 string(6) "banana" 153 ["l"]=> 154 string(5) "lemon" 155 ["o"]=> 156 string(6) "orange" 157 ["o2"]=> 158 string(7) "orange2" 159 ["o20"]=> 160 string(8) "orange20" 161} 162 163-- Testing ksort() by supplying string array (case insensitive), 'flag' = SORT_STRING|SORT_FLAG_CASE -- 164bool(true) 165array(8) { 166 ["b"]=> 167 string(6) "banana" 168 ["l"]=> 169 string(5) "lemon" 170 ["o"]=> 171 string(6) "orange" 172 ["O"]=> 173 string(6) "Orange" 174 ["O1"]=> 175 string(7) "Orange1" 176 ["o2"]=> 177 string(7) "orange2" 178 ["o20"]=> 179 string(8) "orange20" 180 ["O3"]=> 181 string(7) "Orange3" 182} 183 184-- Testing ksort() by supplying string array (natural), 'flag' = SORT_NATURAL -- 185bool(true) 186array(8) { 187 ["O"]=> 188 string(6) "Orange" 189 ["O1"]=> 190 string(7) "Orange1" 191 ["O3"]=> 192 string(7) "Orange3" 193 ["b"]=> 194 string(6) "banana" 195 ["l"]=> 196 string(5) "lemon" 197 ["o"]=> 198 string(6) "orange" 199 ["o2"]=> 200 string(7) "orange2" 201 ["o20"]=> 202 string(8) "orange20" 203} 204 205-- Testing ksort() by supplying string array (natural, case insensitive), 'flag' = SORT_NATURAL|SORT_FLAG_CASE -- 206bool(true) 207array(8) { 208 ["b"]=> 209 string(6) "banana" 210 ["l"]=> 211 string(5) "lemon" 212 ["o"]=> 213 string(6) "orange" 214 ["O"]=> 215 string(6) "Orange" 216 ["O1"]=> 217 string(7) "Orange1" 218 ["o2"]=> 219 string(7) "orange2" 220 ["O3"]=> 221 string(7) "Orange3" 222 ["o20"]=> 223 string(8) "orange20" 224} 225 226-- Testing ksort() by supplying numeric array, 'flag' = SORT_NUMERIC -- 227bool(true) 228array(4) { 229 [22]=> 230 int(1) 231 [33]=> 232 int(3) 233 [100]=> 234 int(4) 235 [555]=> 236 int(2) 237} 238Done 239