Annotation of embedaddon/php/ext/standard/tests/array/uasort_variation11.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test uasort() function : usage variations - different associative arrays
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: /* Testing uasort() with different associative arrays having keys as
11: * string, integer, default & duplicate keys
12: */
13:
14: echo "*** Testing uasort() : sorting different associative arrays ***\n";
15:
16: // comparison function
17: /* Prototype : int cmp(mixed $value1, mixed $value2)
18: * Parameters : $value1 and $value2 - values to be compared
19: * Return value : 0 - if both values are same
20: * 1 - if value1 is greater than value2
21: * -1 - if value1 is less than value2
22: * Description : compares value1 and value2
23: */
24: function cmp($value1, $value2)
25: {
26: if($value1 == $value2) {
27: return 0;
28: }
29: else if($value1 > $value2) {
30: return 1;
31: }
32: else
33: return -1;
34: }
35:
36: // Array with duplicate string and integer keys
37: $array_arg = array(0 => 2, "a" => 8, "d" => 9, 3 => 3, 5 => 2, "o" => 6, "z" => -99, 0 => 1, "z" => 3);
38: echo "-- Array with duplicate keys --\n";
39: var_dump( uasort($array_arg, 'cmp') );
40: var_dump($array_arg);
41:
42: // Array with default and assigned keys
43: $array_arg = array(0 => "Banana", 1 => "Mango", "Orange", 2 => "Apple", "Pineapple");
44: echo "-- Array with default/assigned keys --\n";
45: var_dump( uasort($array_arg, 'cmp') );
46: var_dump($array_arg);
47:
48: echo "Done"
49: ?>
50: --EXPECTF--
51: *** Testing uasort() : sorting different associative arrays ***
52: -- Array with duplicate keys --
53: bool(true)
54: array(7) {
55: [0]=>
56: int(1)
57: [5]=>
58: int(2)
59: ["z"]=>
60: int(3)
61: [3]=>
62: int(3)
63: ["o"]=>
64: int(6)
65: ["a"]=>
66: int(8)
67: ["d"]=>
68: int(9)
69: }
70: -- Array with default/assigned keys --
71: bool(true)
72: array(4) {
73: [2]=>
74: string(5) "Apple"
75: [0]=>
76: string(6) "Banana"
77: [1]=>
78: string(5) "Mango"
79: [3]=>
80: string(9) "Pineapple"
81: }
82: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>