1--TEST-- 2Test asort() function : usage variations - sort array with diff. sub arrays, 'sort_flags' as default/SORT_REGULAR 3--FILE-- 4<?php 5/* Prototype : bool asort ( array &$array [, int $sort_flags] ) 6 * Description: Sort an array and maintain index association. 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 asort() 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 asort() : 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 asort() by supplying various arrays containing sub arrays --\n"; 39 40// loop through to test asort() with different arrays 41foreach ($various_arrays as $array) { 42 43 echo "\n-- Iteration $count --\n"; 44 // testing asort() function by supplying different arrays, flag value is default 45 echo "- With default sort_flag -\n"; 46 $temp_array = $array; 47 var_dump(asort($temp_array) ); 48 var_dump($temp_array); 49 50 // testing asort() function by supplying different arrays, flag value = SORT_REGULAR 51 echo "- Sort_flag = SORT_REGULAR -\n"; 52 $temp_array = $array; 53 var_dump(asort($temp_array, SORT_REGULAR) ); 54 var_dump($temp_array); 55 $count++; 56} 57 58echo "Done\n"; 59?> 60--EXPECTF-- 61*** Testing asort() : usage variations *** 62 63-- Testing asort() 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 ["data[2,1]"]=> 96 int(11) 97 ["data[2,0]"]=> 98 int(44) 99 ["sub_array[2][0] "]=> 100 array(2) { 101 [0]=> 102 int(64) 103 [1]=> 104 int(61) 105 } 106} 107- Sort_flag = SORT_REGULAR - 108bool(true) 109array(3) { 110 ["data[2,1]"]=> 111 int(11) 112 ["data[2,0]"]=> 113 int(44) 114 ["sub_array[2][0] "]=> 115 array(2) { 116 [0]=> 117 int(64) 118 [1]=> 119 int(61) 120 } 121} 122 123-- Iteration 4 -- 124- With default sort_flag - 125bool(true) 126array(4) { 127 ["sub_array[3][3]"]=> 128 array(0) { 129 } 130 ["sub_array[3][1]"]=> 131 array(1) { 132 [0]=> 133 int(11) 134 } 135 ["sub_array[3][2]"]=> 136 array(2) { 137 [0]=> 138 int(22) 139 [1]=> 140 int(-55) 141 } 142 ["sub_array[3][0]"]=> 143 array(3) { 144 [0]=> 145 int(33) 146 [1]=> 147 int(-5) 148 [2]=> 149 int(6) 150 } 151} 152- Sort_flag = SORT_REGULAR - 153bool(true) 154array(4) { 155 ["sub_array[3][3]"]=> 156 array(0) { 157 } 158 ["sub_array[3][1]"]=> 159 array(1) { 160 [0]=> 161 int(11) 162 } 163 ["sub_array[3][2]"]=> 164 array(2) { 165 [0]=> 166 int(22) 167 [1]=> 168 int(-55) 169 } 170 ["sub_array[3][0]"]=> 171 array(3) { 172 [0]=> 173 int(33) 174 [1]=> 175 int(-5) 176 [2]=> 177 int(6) 178 } 179} 180Done 181