1--TEST-- 2Test usort() function : usage variations - numeric data 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 arrays of numeric data to usort() to test how it is re-ordered 12 */ 13 14echo "*** Testing usort() : usage variation ***\n"; 15 16function cmp_function($value1, $value2) 17{ 18 if($value1 == $value2) { 19 return 0; 20 } 21 else if($value1 > $value2) { 22 return 1; 23 } 24 else { 25 return -1; 26 } 27} 28 29// Int array 30$int_values = array(0 => 3, 1 => 2, 3 => 100, 31 4 => 150, 5 => 25, 6 => 350, 32 7 => 0, 8 => -3, 9 => -1200); 33 34echo "\n-- Sorting Integer array --\n"; 35var_dump( usort($int_values, 'cmp_function') ); 36var_dump($int_values); 37 38// Octal array 39$octal_values = array(0 => 056, 1 => 023, 2 => 00, 40 3 => 015, 4 => -045, 5 => 01, 6 => -07); 41 42echo "\n-- Sorting Octal array --\n"; 43var_dump( usort($octal_values, 'cmp_function') ); 44var_dump($octal_values); 45 46// Hexadecimal array 47$hex_values = array(0 => 0xAE, 1 => 0x2B, 2 => 0X10, 48 3 => -0xCF, 4 => 0X12, 5 => -0XF2); 49 50echo "\n-- Sorting Hex array --\n"; 51var_dump( usort($hex_values, 'cmp_function') ); 52var_dump($hex_values); 53 54// Float array 55$float_values = array( 0 => 10.2, 1 => 2.4, 2 => -3.4, 56 3 => 0, 4 => 0.5, 5 => 7.3e3, 6 => -9.34E-2); 57 58echo "\n-- Sorting Float array --\n"; 59var_dump( usort($float_values, 'cmp_function') ); 60var_dump($float_values); 61 62// empty array 63$empty_array = array(); 64 65echo "\n-- Sorting empty array --\n"; 66var_dump( usort($empty_array, 'cmp_function') ); 67var_dump($empty_array); 68?> 69===DONE=== 70--EXPECT-- 71*** Testing usort() : usage variation *** 72 73-- Sorting Integer array -- 74bool(true) 75array(9) { 76 [0]=> 77 int(-1200) 78 [1]=> 79 int(-3) 80 [2]=> 81 int(0) 82 [3]=> 83 int(2) 84 [4]=> 85 int(3) 86 [5]=> 87 int(25) 88 [6]=> 89 int(100) 90 [7]=> 91 int(150) 92 [8]=> 93 int(350) 94} 95 96-- Sorting Octal array -- 97bool(true) 98array(7) { 99 [0]=> 100 int(-37) 101 [1]=> 102 int(-7) 103 [2]=> 104 int(0) 105 [3]=> 106 int(1) 107 [4]=> 108 int(13) 109 [5]=> 110 int(19) 111 [6]=> 112 int(46) 113} 114 115-- Sorting Hex array -- 116bool(true) 117array(6) { 118 [0]=> 119 int(-242) 120 [1]=> 121 int(-207) 122 [2]=> 123 int(16) 124 [3]=> 125 int(18) 126 [4]=> 127 int(43) 128 [5]=> 129 int(174) 130} 131 132-- Sorting Float array -- 133bool(true) 134array(7) { 135 [0]=> 136 float(-3.4) 137 [1]=> 138 float(-0.0934) 139 [2]=> 140 int(0) 141 [3]=> 142 float(0.5) 143 [4]=> 144 float(2.4) 145 [5]=> 146 float(10.2) 147 [6]=> 148 float(7300) 149} 150 151-- Sorting empty array -- 152bool(true) 153array(0) { 154} 155===DONE=== 156