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