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