summaryrefslogtreecommitdiff
path: root/ext/standard/tests/array/usort_variation3.phpt
blob: 3758fe9c9a628e9ce0db0487b3a7a87554909d4a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
--TEST--
Test usort() function : usage variations - diff. array values
--FILE--
<?php
/* Prototype  : bool usort(array $array_arg, string $cmp_function)
 * Description: Sort an array by values using a user-defined comparison function 
 * Source code: ext/standard/array.c
 */

/*
 * Pass an array with different data types as keys to usort() to test how it is re-ordered
 */

echo "*** Testing usort() : usage variation ***\n";

function cmp_function($value1, $value2)
{
  if($value1 == $value2) {
    return 0;
  }
  else if($value1 > $value2) {
    return -1;
  }
  else {
    return 1;
  }
}

// different heredoc strings
  
// single line heredoc string
$simple_heredoc = <<<EOT2
simple
EOT2;
  
// multiline heredoc string
$multiline_heredoc = <<<EOT3
multiline heredoc with 123
and speci@! ch@r..\ncheck\talso
EOT3;
  
$array_arg = array(
  // numeric keys
  -2 => 9,
  8.9 => 8, 
  012 => 7,
  0x34 => 6,

  // string keys
  'key' => 5,  //single quoted key
  "two" => 4,  //double quoted key 
  " " => 0,  // space as key
  
  // bool keys
  TRUE => 100, 
  FALSE => 25,

  // null keys
  NULL => 35,

  // binary key
  "a".chr(0)."b" => 45,
  b"binary" => 30,

  //heredoc keys
  $simple_heredoc => 75,
  $multiline_heredoc => 200,
  
  // default key
  1,
);

var_dump( usort($array_arg, 'cmp_function') );
echo "\n-- Sorted array after usort() function call --\n";
var_dump($array_arg);
?>
===DONE===
--EXPECTF--
*** Testing usort() : usage variation ***
bool(true)

-- Sorted array after usort() function call --
array(15) {
  [0]=>
  int(200)
  [1]=>
  int(100)
  [2]=>
  int(75)
  [3]=>
  int(45)
  [4]=>
  int(35)
  [5]=>
  int(30)
  [6]=>
  int(25)
  [7]=>
  int(9)
  [8]=>
  int(8)
  [9]=>
  int(7)
  [10]=>
  int(6)
  [11]=>
  int(5)
  [12]=>
  int(4)
  [13]=>
  int(1)
  [14]=>
  int(0)
}
===DONE===