]> granicus.if.org Git - php/blob
d27cd569c0
[php] /
1 --TEST--
2 Test uasort() function : usage variations - built-in 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 built-in library functions in place of 'cmp_function'
12 *   valid comparison functions: strcmp() & strcasecmp()
13 */
14
15 echo "*** Testing uasort() : built in function as 'cmp_function' ***\n";
16 // Initializing variables
17 $array_arg = array("b" => "Banana", "m" => "Mango", "a" => "apple", "p" => "Pineapple", "o" => "orange");
18 $builtin_fun_arg = $array_arg;
19 $languageConstruct_fun_arg = $array_arg;
20
21 // Testing library functions as comparison function
22 echo "-- Testing uasort() with built-in 'cmp_function': strcasecmp() --\n";
23 var_dump( uasort($builtin_fun_arg, 'strcasecmp') );  // expecting: bool(true)
24 var_dump($builtin_fun_arg);
25
26 echo "-- Testing uasort() with built-in 'cmp_function': strcmp() --\n";
27 var_dump( uasort($array_arg, 'strcmp') );  // expecting: bool(true)
28 var_dump($array_arg);
29
30 echo "Done"
31 ?>
32 --EXPECTF--
33 *** Testing uasort() : built in function as 'cmp_function' ***
34 -- Testing uasort() with built-in 'cmp_function': strcasecmp() --
35 bool(true)
36 array(5) {
37   ["a"]=>
38   string(5) "apple"
39   ["b"]=>
40   string(6) "Banana"
41   ["m"]=>
42   string(5) "Mango"
43   ["o"]=>
44   string(6) "orange"
45   ["p"]=>
46   string(9) "Pineapple"
47 }
48 -- Testing uasort() with built-in 'cmp_function': strcmp() --
49 bool(true)
50 array(5) {
51   ["b"]=>
52   string(6) "Banana"
53   ["m"]=>
54   string(5) "Mango"
55   ["p"]=>
56   string(9) "Pineapple"
57   ["a"]=>
58   string(5) "apple"
59   ["o"]=>
60   string(6) "orange"
61 }
62 Done