1--TEST-- 2Test usort() function : usage variations - multi-dimensional arrays 3--FILE-- 4<?php 5/* Prototype : bool usort(array $array_arg, string $cmp_function) 6 * Description: Sort an array by values using a user-defined comparison function 7 * Source code: ext/standard/array.c 8 */ 9 10/* 11 * Pass a multi-dimensional array as $array_arg argument to usort() 12 * to test how array is re-ordered 13 */ 14 15echo "*** Testing usort() : usage variation ***\n"; 16 17function cmp_function($value1, $value2) 18{ 19 if($value1 == $value2) { 20 return 0; 21 } 22 else if($value1 > $value2) { 23 return 1; 24 } 25 else { 26 return -1; 27 } 28} 29 30$array_args = array( 31 0 => array(2, 10, -1), 32 1 => array(100), 33 2 => array(), 34 3 => array(0), 35 4 => array(-1), 36 5 => array(-9, 34, 54, 0, 20), 37 6 => array(''), 38 7 => array("apple", "Apple", "APPLE", "aPPle", "aPpLe") 39); 40 41$temp_array = $array_args; 42 43echo "\n-- Pass usort() a two-dimensional array --\n"; 44// sorting array_arg as whole array 45var_dump( usort($temp_array, 'cmp_function') ); 46 47echo "-- Array after call to usort() --\n"; 48var_dump($temp_array); 49 50echo "\n-- Pass usort() a sub-array --\n"; 51var_dump( usort($array_args[5], 'cmp_function') ); 52 53echo "-- Array after call to usort() --\n"; 54var_dump($array_args[5]); 55?> 56===DONE=== 57--EXPECTF-- 58*** Testing usort() : usage variation *** 59 60-- Pass usort() a two-dimensional array -- 61bool(true) 62-- Array after call to usort() -- 63array(8) { 64 [0]=> 65 array(0) { 66 } 67 [1]=> 68 array(1) { 69 [0]=> 70 int(-1) 71 } 72 [2]=> 73 array(1) { 74 [0]=> 75 string(0) "" 76 } 77 [3]=> 78 array(1) { 79 [0]=> 80 int(0) 81 } 82 [4]=> 83 array(1) { 84 [0]=> 85 int(100) 86 } 87 [5]=> 88 array(3) { 89 [0]=> 90 int(2) 91 [1]=> 92 int(10) 93 [2]=> 94 int(-1) 95 } 96 [6]=> 97 array(5) { 98 [0]=> 99 int(-9) 100 [1]=> 101 int(34) 102 [2]=> 103 int(54) 104 [3]=> 105 int(0) 106 [4]=> 107 int(20) 108 } 109 [7]=> 110 array(5) { 111 [0]=> 112 string(5) "apple" 113 [1]=> 114 string(5) "Apple" 115 [2]=> 116 string(5) "APPLE" 117 [3]=> 118 string(5) "aPPle" 119 [4]=> 120 string(5) "aPpLe" 121 } 122} 123 124-- Pass usort() a sub-array -- 125bool(true) 126-- Array after call to usort() -- 127array(5) { 128 [0]=> 129 int(-9) 130 [1]=> 131 int(0) 132 [2]=> 133 int(20) 134 [3]=> 135 int(34) 136 [4]=> 137 int(54) 138} 139===DONE=== 140