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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
|
--TEST--
Test arsort() function : basic functionality
--FILE--
<?php
/* Prototype : bool arsort ( array &$array [, int $sort_flags] )
* Description: Sort an array and maintain index association
Elements will be arranged from highest to lowest when this function has completed.
* Source code: ext/standard/array.c
*/
/*
* Testing arsort() by providing integer/string arrays to check the basic functionality
* with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
* SORT_STRING - compare items as strings
*/
echo "*** Testing arsort() : basic functionality ***\n";
// an array containing unsorted string values with indices
$unsorted_strings = array( "l" => "lemon", "o" => "orange", "b" => "banana" );
// an array containing unsorted numeric values with indices
$unsorted_numerics = array( 1 => 100, 2 => 33, 3 => 555, 4 => 22 );
echo "\n-- Testing arsort() by supplying string array, 'flag' value is default --\n";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array) ); // expecting : bool(true)
var_dump( $temp_array);
echo "\n-- Testing arsort() by supplying numeric array, 'flag' value is default --\n";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array) ); // expecting : bool(true)
var_dump( $temp_array);
echo "\n-- Testing arsort() by supplying string array, 'flag' = SORT_REGULAR --\n";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
var_dump( $temp_array);
echo "\n-- Testing arsort() by supplying numeric array, 'flag' = SORT_REGULAR --\n";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array, SORT_REGULAR) ); // expecting : bool(true)
var_dump( $temp_array);
echo "\n-- Testing arsort() by supplying string array, 'flag' = SORT_STRING --\n";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array, SORT_STRING) ); // expecting : bool(true)
var_dump( $temp_array);
echo "\n-- Testing arsort() by supplying numeric array, 'flag' = SORT_NUMERIC --\n";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array, SORT_NUMERIC) ); // expecting : bool(true)
var_dump( $temp_array);
echo "Done\n";
?>
--EXPECTF--
*** Testing arsort() : basic functionality ***
-- Testing arsort() by supplying string array, 'flag' value is default --
bool(true)
array(3) {
["o"]=>
string(6) "orange"
["l"]=>
string(5) "lemon"
["b"]=>
string(6) "banana"
}
-- Testing arsort() by supplying numeric array, 'flag' value is default --
bool(true)
array(4) {
[3]=>
int(555)
[1]=>
int(100)
[2]=>
int(33)
[4]=>
int(22)
}
-- Testing arsort() by supplying string array, 'flag' = SORT_REGULAR --
bool(true)
array(3) {
["o"]=>
string(6) "orange"
["l"]=>
string(5) "lemon"
["b"]=>
string(6) "banana"
}
-- Testing arsort() by supplying numeric array, 'flag' = SORT_REGULAR --
bool(true)
array(4) {
[3]=>
int(555)
[1]=>
int(100)
[2]=>
int(33)
[4]=>
int(22)
}
-- Testing arsort() by supplying string array, 'flag' = SORT_STRING --
bool(true)
array(3) {
["o"]=>
string(6) "orange"
["l"]=>
string(5) "lemon"
["b"]=>
string(6) "banana"
}
-- Testing arsort() by supplying numeric array, 'flag' = SORT_NUMERIC --
bool(true)
array(4) {
[3]=>
int(555)
[1]=>
int(100)
[2]=>
int(33)
[4]=>
int(22)
}
Done
|