1--TEST-- 2Test uasort() function : usage variations - anonymous function as 'cmp_function' 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* Passing different anonymous functions as 'cmp_function' 12* arguments passed by value 13* arguments passed by reference 14*/ 15 16echo "*** Testing uasort() : anonymous function as 'cmp_function' ***\n"; 17 18$cmp_function = function($value1, $value2) { 19 if ($value1 == $value2) { return 0; } 20 else if ($value1 > $value2) { return 1; } 21 else { return -1; } 22}; 23 24$array_arg = array(0 => 100, 1 => 3, 2 => -70, 3 => 24, 4 => 90); 25echo "-- Anonymous 'cmp_function' with parameters passed by value --\n"; 26var_dump( uasort($array_arg, $cmp_function) ); 27var_dump($array_arg); 28 29$cmp_function = function(&$value1, &$value2) { 30 if ($value1 == $value2) { return 0; } 31 else if ($value1 > $value2) { return 1; } 32 else { return -1; } 33}; 34 35$array_arg = array("b" => "Banana", "m" => "Mango", "a" => "Apple", "p" => "Pineapple"); 36echo "-- Anonymous 'cmp_function' with parameters passed by reference --\n"; 37var_dump( uasort($array_arg, $cmp_function ) ); 38var_dump($array_arg); 39 40echo "Done" 41?> 42--EXPECTF-- 43*** Testing uasort() : anonymous function as 'cmp_function' *** 44-- Anonymous 'cmp_function' with parameters passed by value -- 45bool(true) 46array(5) { 47 [2]=> 48 int(-70) 49 [1]=> 50 int(3) 51 [3]=> 52 int(24) 53 [4]=> 54 int(90) 55 [0]=> 56 int(100) 57} 58-- Anonymous 'cmp_function' with parameters passed by reference -- 59bool(true) 60array(4) { 61 ["a"]=> 62 string(5) "Apple" 63 ["b"]=> 64 string(6) "Banana" 65 ["m"]=> 66 string(5) "Mango" 67 ["p"]=> 68 string(9) "Pineapple" 69} 70Done 71