1--TEST-- 2Test arsort() function : usage variations - sort array with diff. sub arrays, '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 contains sub arrays for $array argument with flowing flag values 13 * flag value as default 14 * SORT_REGULAR - compare items normally 15 * Note: arrays are sorted based on total count of elements inside it, when all the elements are arrays 16*/ 17 18echo "*** Testing arsort() : usage variations ***\n"; 19 20// array of arrays 21$various_arrays = array ( 22 // null array 23 "array[0]" => array(), 24 25 // array contains null sub array 26 "array[1]" => array( "sub_array[1][0]" => array() ), 27 28 // array of arrays along with some values 29 "array[2]" => array("data[2,0]" => 44, "data[2,1]" => 11, "sub_array[2][0] " => array(64,61) ), 30 31 // array contains sub arrays 32 "array[3]" => array ( "sub_array[3][0]" => array(33,-5,6), "sub_array[3][1]" => array(11), 33 "sub_array[3][2]" => array(22,-55), "sub_array[3][3]" => array() ) 34); 35 36 37$count = 1; 38echo "\n-- Testing arsort() by supplying various arrays containing sub arrays --\n"; 39 40// loop through to test arsort() with different arrays 41foreach ($various_arrays as $array) { 42 43 echo "\n-- Iteration $count --\n"; 44 // testing arsort() function by supplying different arrays, flag value is default 45 echo "- With default sort_flag -\n"; 46 $temp_array = $array; 47 var_dump(arsort($temp_array) ); 48 var_dump($temp_array); 49 50 // testing arsort() function by supplying different arrays, flag value = SORT_REGULAR 51 echo "- Sort_flag = SORT_REGULAR -\n"; 52 $temp_array = $array; 53 var_dump(arsort($temp_array, SORT_REGULAR) ); 54 var_dump($temp_array); 55 $count++; 56} 57 58echo "Done\n"; 59?> 60--EXPECTF-- 61*** Testing arsort() : usage variations *** 62 63-- Testing arsort() by supplying various arrays containing sub arrays -- 64 65-- Iteration 1 -- 66- With default sort_flag - 67bool(true) 68array(0) { 69} 70- Sort_flag = SORT_REGULAR - 71bool(true) 72array(0) { 73} 74 75-- Iteration 2 -- 76- With default sort_flag - 77bool(true) 78array(1) { 79 ["sub_array[1][0]"]=> 80 array(0) { 81 } 82} 83- Sort_flag = SORT_REGULAR - 84bool(true) 85array(1) { 86 ["sub_array[1][0]"]=> 87 array(0) { 88 } 89} 90 91-- Iteration 3 -- 92- With default sort_flag - 93bool(true) 94array(3) { 95 ["sub_array[2][0] "]=> 96 array(2) { 97 [0]=> 98 int(64) 99 [1]=> 100 int(61) 101 } 102 ["data[2,0]"]=> 103 int(44) 104 ["data[2,1]"]=> 105 int(11) 106} 107- Sort_flag = SORT_REGULAR - 108bool(true) 109array(3) { 110 ["sub_array[2][0] "]=> 111 array(2) { 112 [0]=> 113 int(64) 114 [1]=> 115 int(61) 116 } 117 ["data[2,0]"]=> 118 int(44) 119 ["data[2,1]"]=> 120 int(11) 121} 122 123-- Iteration 4 -- 124- With default sort_flag - 125bool(true) 126array(4) { 127 ["sub_array[3][0]"]=> 128 array(3) { 129 [0]=> 130 int(33) 131 [1]=> 132 int(-5) 133 [2]=> 134 int(6) 135 } 136 ["sub_array[3][2]"]=> 137 array(2) { 138 [0]=> 139 int(22) 140 [1]=> 141 int(-55) 142 } 143 ["sub_array[3][1]"]=> 144 array(1) { 145 [0]=> 146 int(11) 147 } 148 ["sub_array[3][3]"]=> 149 array(0) { 150 } 151} 152- Sort_flag = SORT_REGULAR - 153bool(true) 154array(4) { 155 ["sub_array[3][0]"]=> 156 array(3) { 157 [0]=> 158 int(33) 159 [1]=> 160 int(-5) 161 [2]=> 162 int(6) 163 } 164 ["sub_array[3][2]"]=> 165 array(2) { 166 [0]=> 167 int(22) 168 [1]=> 169 int(-55) 170 } 171 ["sub_array[3][1]"]=> 172 array(1) { 173 [0]=> 174 int(11) 175 } 176 ["sub_array[3][3]"]=> 177 array(0) { 178 } 179} 180Done 181