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