1--TEST-- 2Test arsort() function : usage variations - sorting arrays with/without keys, 'sort_flags' as default/SORT_REGULAR 3--FILE-- 4<?php 5/* Prototype : bool arsort ( array &$array [, int $sort_flags] ) 6 * Description: Sort an array and maintain index association. 7 Elements will be arranged from highest to lowest when this function has completed. 8 * Source code: ext/standard/array.c 9*/ 10 11/* 12 * Testing arsort() by providing arrays with key values for $array argument with following flag values. 13 * 1.flag value as default 14 * 2.SORT_REGULAR - compare items normally 15 */ 16 17echo "*** Testing arsort() : usage variations ***\n"; 18 19// list of arrays with/without key values 20$various_arrays = array ( 21 array(5 => 55, 66, 22, 33, 11), 22 array ("a" => "orange", "banana", "c" => "apple"), 23 array(1, 2, 3, 4, 5, 6), 24 array("first", 5 => "second", "third"), 25 array(1, 1, 8 => 1, 4 => 1, 19, 3 => 13), 26 array('bar' => 'baz', "foo" => 1), 27 array('a'=>1,'b'=>array('e'=>2,'f'=>3),'c'=>array('g'=>4),'d'=>5), 28); 29 30$count = 1; 31echo "\n-- Testing arsort() by supplying various arrays with key values --\n"; 32 33// loop through to test arsort() with different arrays, 34// to test the new keys for the elements in the sorted array 35foreach ($various_arrays as $array) { 36 echo "\n-- Iteration $count --\n"; 37 38 echo "- With default sort_flag -\n"; 39 $temp_array = $array; 40 var_dump(arsort($temp_array) ); 41 var_dump($temp_array); 42 43 echo "- Sort_flag = SORT_REGULAR -\n"; 44 $temp_array = $array; 45 var_dump(arsort($temp_array, SORT_REGULAR) ); 46 var_dump($temp_array); 47 $count++; 48} 49 50echo "Done\n"; 51?> 52--EXPECTF-- 53*** Testing arsort() : usage variations *** 54 55-- Testing arsort() by supplying various arrays with key values -- 56 57-- Iteration 1 -- 58- With default sort_flag - 59bool(true) 60array(5) { 61 [6]=> 62 int(66) 63 [5]=> 64 int(55) 65 [8]=> 66 int(33) 67 [7]=> 68 int(22) 69 [9]=> 70 int(11) 71} 72- Sort_flag = SORT_REGULAR - 73bool(true) 74array(5) { 75 [6]=> 76 int(66) 77 [5]=> 78 int(55) 79 [8]=> 80 int(33) 81 [7]=> 82 int(22) 83 [9]=> 84 int(11) 85} 86 87-- Iteration 2 -- 88- With default sort_flag - 89bool(true) 90array(3) { 91 ["a"]=> 92 string(6) "orange" 93 [0]=> 94 string(6) "banana" 95 ["c"]=> 96 string(5) "apple" 97} 98- Sort_flag = SORT_REGULAR - 99bool(true) 100array(3) { 101 ["a"]=> 102 string(6) "orange" 103 [0]=> 104 string(6) "banana" 105 ["c"]=> 106 string(5) "apple" 107} 108 109-- Iteration 3 -- 110- With default sort_flag - 111bool(true) 112array(6) { 113 [5]=> 114 int(6) 115 [4]=> 116 int(5) 117 [3]=> 118 int(4) 119 [2]=> 120 int(3) 121 [1]=> 122 int(2) 123 [0]=> 124 int(1) 125} 126- Sort_flag = SORT_REGULAR - 127bool(true) 128array(6) { 129 [5]=> 130 int(6) 131 [4]=> 132 int(5) 133 [3]=> 134 int(4) 135 [2]=> 136 int(3) 137 [1]=> 138 int(2) 139 [0]=> 140 int(1) 141} 142 143-- Iteration 4 -- 144- With default sort_flag - 145bool(true) 146array(3) { 147 [6]=> 148 string(5) "third" 149 [5]=> 150 string(6) "second" 151 [0]=> 152 string(5) "first" 153} 154- Sort_flag = SORT_REGULAR - 155bool(true) 156array(3) { 157 [6]=> 158 string(5) "third" 159 [5]=> 160 string(6) "second" 161 [0]=> 162 string(5) "first" 163} 164 165-- Iteration 5 -- 166- With default sort_flag - 167bool(true) 168array(6) { 169 [9]=> 170 int(19) 171 [3]=> 172 int(13) 173 [0]=> 174 int(1) 175 [1]=> 176 int(1) 177 [8]=> 178 int(1) 179 [4]=> 180 int(1) 181} 182- Sort_flag = SORT_REGULAR - 183bool(true) 184array(6) { 185 [9]=> 186 int(19) 187 [3]=> 188 int(13) 189 [0]=> 190 int(1) 191 [1]=> 192 int(1) 193 [8]=> 194 int(1) 195 [4]=> 196 int(1) 197} 198 199-- Iteration 6 -- 200- With default sort_flag - 201bool(true) 202array(2) { 203 ["foo"]=> 204 int(1) 205 ["bar"]=> 206 string(3) "baz" 207} 208- Sort_flag = SORT_REGULAR - 209bool(true) 210array(2) { 211 ["foo"]=> 212 int(1) 213 ["bar"]=> 214 string(3) "baz" 215} 216 217-- Iteration 7 -- 218- With default sort_flag - 219bool(true) 220array(4) { 221 ["b"]=> 222 array(2) { 223 ["e"]=> 224 int(2) 225 ["f"]=> 226 int(3) 227 } 228 ["c"]=> 229 array(1) { 230 ["g"]=> 231 int(4) 232 } 233 ["d"]=> 234 int(5) 235 ["a"]=> 236 int(1) 237} 238- Sort_flag = SORT_REGULAR - 239bool(true) 240array(4) { 241 ["b"]=> 242 array(2) { 243 ["e"]=> 244 int(2) 245 ["f"]=> 246 int(3) 247 } 248 ["c"]=> 249 array(1) { 250 ["g"]=> 251 int(4) 252 } 253 ["d"]=> 254 int(5) 255 ["a"]=> 256 int(1) 257} 258Done 259