1--TEST--
2Test usort() function : usage variations - duplicate keys and values
3--FILE--
4<?php
5/* Prototype  : bool usort(array $array_arg, string $cmp_function)
6 * Description: Sort an array by values using a user-defined comparison function
7 * Source code: ext/standard/array.c
8 */
9
10/*
11 * Pass an array with duplicate keys and values to usort() to test behaviour
12 */
13
14echo "*** Testing usort() : usage variation ***\n";
15
16function cmp($value1, $value2)
17{
18  if($value1 == $value2) {
19    return 0;
20  }
21  else if($value1 > $value2) {
22    return 1;
23  }
24  else
25    return -1;
26}
27
28// Array with duplicate string and integer keys and values
29$array_arg = array(0 => 2,     "a" => 8, "d" => 9,
30                   3 => 3,     5 => 2,   "o" => 6,
31                   "z" => -99, 0 => 1,   "z" => 3);
32
33echo "\n-- Array with duplicate keys --\n";
34var_dump( usort($array_arg, 'cmp') );
35var_dump($array_arg);
36
37// Array with default and assigned keys
38$array_arg = array(0 => "Banana", 1 => "Mango", "Orange", 2 => "Apple", "Pineapple");
39
40echo "\n-- Array with default/assigned keys --\n";
41var_dump( usort($array_arg, 'cmp') );
42var_dump($array_arg);
43?>
44===DONE===
45--EXPECTF--
46*** Testing usort() : usage variation ***
47
48-- Array with duplicate keys --
49bool(true)
50array(7) {
51  [0]=>
52  int(1)
53  [1]=>
54  int(2)
55  [2]=>
56  int(3)
57  [3]=>
58  int(3)
59  [4]=>
60  int(6)
61  [5]=>
62  int(8)
63  [6]=>
64  int(9)
65}
66
67-- Array with default/assigned keys --
68bool(true)
69array(4) {
70  [0]=>
71  string(5) "Apple"
72  [1]=>
73  string(6) "Banana"
74  [2]=>
75  string(5) "Mango"
76  [3]=>
77  string(9) "Pineapple"
78}
79===DONE===
80