1--TEST-- 2Test uasort() function : usage variations - sort diff. strings 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* sorting different strings: 12* single quoted, double quoted and heredoc strings 13*/ 14 15// comparison function 16/* Prototype : int cmp_function(mixed $value1, mixed $value2) 17 * Parameters : $value1 and $value2 - values to be compared 18 * Return value : 0 - if both values are same 19 * 1 - if value1 is greater than value2 20 * -1 - if value1 is less than value2 21 * Description : compares value1 and value2 22 */ 23function cmp_function($value1, $value2) 24{ 25 if($value1 == $value2) { 26 return 0; 27 } 28 else if($value1 > $value2) { 29 return 1; 30 } 31 else { 32 return -1; 33 } 34} 35 36// Different heredoc strings to be sorted 37$empty_heredoc =<<<EOT 38EOT; 39 40$simple_heredoc1 =<<<EOT 41Heredoc 42EOT; 43 44$simple_heredoc2 =<<<EOT 45HEREDOC 46EOT; 47 48$multiline_heredoc =<<<EOT 49heredoc string\twith!@# and 123 50Test this!!! 51EOT; 52 53 54echo "*** Testing uasort() : different string arrays as 'array_arg' ***\n"; 55 56// Single quoted strings 57$single_quoted_values = array( 58 0 => ' ', 1 => 'test', 3 => 'Hello', 4 => 'HELLO', 59 5 => '', 6 => '\t', 7 => '0', 8 => '123Hello', 9 => '\'', 10 => '@#$%' 60); 61echo "-- Sorting Single Quoted String values --\n"; 62var_dump( uasort($single_quoted_values, 'cmp_function') ); // expecting: bool(true) 63var_dump($single_quoted_values); 64 65// Double quoted strings 66$double_quoted_values = array( 67 0 => " ", 1 => "test", 3 => "Hello", 4 => "HELLO", 68 5 => "", 6 => "\t", 7 => "0", 8 => "123Hello", 9 => "\"", 10 => "@#$%" 69); 70echo "-- Sorting Double Quoted String values --\n"; 71var_dump( uasort($double_quoted_values, 'cmp_function') ); // expecting: bool(true) 72var_dump($double_quoted_values); 73 74// Heredoc strings 75$heredoc_values = array(0 => $empty_heredoc, 1 => $simple_heredoc1, 2 => $simple_heredoc2, 3 => $multiline_heredoc); 76echo "-- Sorting Heredoc String values --\n"; 77var_dump( uasort($heredoc_values, 'cmp_function') ); // expecting: bool(true) 78var_dump($heredoc_values); 79 80echo "Done" 81?> 82--EXPECTF-- 83*** Testing uasort() : different string arrays as 'array_arg' *** 84-- Sorting Single Quoted String values -- 85bool(true) 86array(10) { 87 [5]=> 88 string(0) "" 89 [0]=> 90 string(1) " " 91 [9]=> 92 string(1) "'" 93 [7]=> 94 string(1) "0" 95 [8]=> 96 string(8) "123Hello" 97 [10]=> 98 string(4) "@#$%" 99 [4]=> 100 string(5) "HELLO" 101 [3]=> 102 string(5) "Hello" 103 [6]=> 104 string(2) "\t" 105 [1]=> 106 string(4) "test" 107} 108-- Sorting Double Quoted String values -- 109bool(true) 110array(10) { 111 [5]=> 112 string(0) "" 113 [6]=> 114 string(1) " " 115 [0]=> 116 string(1) " " 117 [9]=> 118 string(1) """ 119 [7]=> 120 string(1) "0" 121 [8]=> 122 string(8) "123Hello" 123 [10]=> 124 string(4) "@#$%" 125 [4]=> 126 string(5) "HELLO" 127 [3]=> 128 string(5) "Hello" 129 [1]=> 130 string(4) "test" 131} 132-- Sorting Heredoc String values -- 133bool(true) 134array(4) { 135 [0]=> 136 string(0) "" 137 [2]=> 138 string(7) "HEREDOC" 139 [1]=> 140 string(7) "Heredoc" 141 [3]=> 142 string(4%d) "heredoc string with!@# and 123 143Test this!!!" 144} 145Done 146