Annotation of embedaddon/php/ext/standard/tests/array/uasort_variation7.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test 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:
16: echo "*** 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);
21: echo "-- Anonymous 'cmp_function' with parameters passed by value --\n";
22: var_dump( uasort($array_arg, create_function('$value1, $value2',$cmp_function) ) );
23: var_dump($array_arg);
24:
25: $array_arg = array("b" => "Banana", "m" => "Mango", "a" => "Apple", "p" => "Pineapple");
26: echo "-- Anonymous 'cmp_function' with parameters passed by reference --\n";
27: var_dump( uasort($array_arg, create_function('&$value1, &$value2', $cmp_function) ) );
28: var_dump($array_arg);
29:
30: echo "Done"
31: ?>
32: --EXPECTF--
33: *** Testing uasort() : anonymous function as 'cmp_function' ***
34: -- Anonymous 'cmp_function' with parameters passed by value --
35: bool(true)
36: array(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 --
49: bool(true)
50: array(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: }
60: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>