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