2 /* Prototype : bool usort(&array $array_arg, string $cmp_function)
3 * Description: Sort an array by values using a user-defined comparison function
4 * Source code: ext/standard/array.c
8 * Pass arrays of string data to usort() to test how it is re-ordered
11 function cmp_function($value1, $value2)
13 if($value1 == $value2) {
16 else if($value1 > $value2) {
23 <<__EntryPoint
>> function main(): void
{
24 echo "*** Testing usort() : usage variation ***\n";
26 // Different heredoc strings to be sorted
27 $empty_heredoc =<<<EOT
30 $simple_heredoc1 =<<<EOT
34 $simple_heredoc2 =<<<EOT
38 $multiline_heredoc =<<<EOT
39 heredoc string\twith!@# and 123
43 // Single quoted strings
44 $single_quoted_values = array(
45 0 => ' ', 1 => 'test', 3 => 'Hello', 4 => 'HELLO',
46 5 => '', 6 => '\t', 7 => '0', 8 => '123Hello',
47 9 => '\'', 10 => '@#$%'
50 echo "\n-- Sorting Single Quoted String values --\n";
51 var_dump( usort(inout
$single_quoted_values, fun('cmp_function')) );
52 var_dump($single_quoted_values);
54 // Double quoted strings
55 $double_quoted_values = array(
56 0 => " ", 1 => "test", 3 => "Hello", 4 => "HELLO",
57 5 => "", 6 => "\t", 7 => "0", 8 => "123Hello",
58 9 => "\"", 10 => "@#$%"
61 echo "\n-- Sorting Double Quoted String values --\n";
62 var_dump( usort(inout
$double_quoted_values, fun('cmp_function')) );
63 var_dump($double_quoted_values);
66 $heredoc_values = array(0 => $empty_heredoc, 1 => $simple_heredoc1,
67 2 => $simple_heredoc2, 3 => $multiline_heredoc);
69 echo "\n-- Sorting Heredoc String values --\n";
70 var_dump( usort(inout
$heredoc_values, fun('cmp_function')) );
71 var_dump($heredoc_values);