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