1--TEST-- 2Test uasort() function : usage variations - sort array with reference variables 3--FILE-- 4<?php 5/* Prototype : bool uasort(array $array_arg, string $cmp_function) 6 * Description: Sort an array with a user-defined comparison function and maintain index association 7 * Source code: ext/standard/array.c 8*/ 9 10/* 11* Testing uasort() with 'array_arg' containing different reference variables 12*/ 13 14// comparison function 15/* Prototype : int cmp_function(mixed $value1, mixed $value2) 16 * Parameters : $value1 and $value2 - values to be compared 17 * Return value : 0 - if both values are same 18 * 1 - if value1 is greater than value2 19 * -1 - if value1 is less than value2 20 * Description : compares value1 and value2 21 */ 22function cmp_function($value1, $value2) 23{ 24 if($value1 == $value2) { 25 return 0; 26 } 27 else if($value1 > $value2) { 28 return 1; 29 } 30 else { 31 return -1; 32 } 33} 34 35echo "*** Testing uasort() : 'array_arg' with elements as reference ***\n"; 36 37// different variables which are used as elements of 'array_arg' 38$value1 = -5; 39$value2 = 100; 40$value3 = 0; 41$value4 = &$value1; 42 43// array_args an array containing elements with reference variables 44$array_arg = array( 45 0 => 10, 46 1 => &$value4, 47 2 => &$value2, 48 3 => 200, 49 4 => &$value3, 50); 51 52echo "-- Sorting 'array_arg' containing different references --\n"; 53var_dump( uasort($array_arg, 'cmp_function') ); // expecting: bool(true) 54var_dump($array_arg); 55 56echo "Done" 57?> 58--EXPECTF-- 59*** Testing uasort() : 'array_arg' with elements as reference *** 60-- Sorting 'array_arg' containing different references -- 61bool(true) 62array(5) { 63 [1]=> 64 &int(-5) 65 [4]=> 66 &int(0) 67 [0]=> 68 int(10) 69 [2]=> 70 &int(100) 71 [3]=> 72 int(200) 73} 74Done 75