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