<?php
bool arsort ( arr &$array [, int $sort_flags = SORT_REGULAR ] )
where,
&$array = The input ARRAY
$sort_flags = To control de sorting behavior
( SEE the below TABLE )
?>
CONSTANT | VALUE | OBSERVATIONS | DEFAULT |
SORT_REGULAR | 0 | compare items normally (don't change types). |
SORT_REGULAR |
SORT_NUMERIC | 1 | compare items numerically | |
SORT_STRING | 2 | compare items as strings | |
SORT_LOCALE_STRING | 5 | compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale. |
|
SORT_NATURAL | 6 | compare items as strings using "natural ordering" like natsort. | |
SORT_FLAG_CASE | 8 | can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively | |
ed48 |
<?php
$arr01s = ["x", "Z", "a", "y", "B", "k"];
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
if(arsort($arr01s) == true)
{
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
}
?>
<?php
$arr02s = ["x", "Z", "a", "y", "B", "k", 5, 7, "9", "11"];
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
if(arsort($arr02s, SORT_REGULAR) == true)
{
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
}
?>
<?php
$arr03s = ["R" => 'red', "G" => 'green', "B" => 'blue', "H" => 'HUE', "S" => 'SATURATION', "K" => 'BLACK'];
echo '<pre>';
var_dump($arr03s);
echo '</pre>';
if(arsort($arr03s, SORT_STRING) == true)
{
echo '<pre>';
var_dump($arr03s);
echo '</pre>';
}
?>
<?php
$arr04s = [1 => 100, 2 => 73, 'THREE' => 120,
'4' => 88, 'five' => '77', 6 => '120',
'8' => 'EIGHT', 9 => 'nine'];
echo '<pre>';
var_dump($arr04s);
echo '</pre>';
if(arsort($arr04s, SORT_NUMERIC) == true)
{
echo '<pre>';
var_dump($arr04s);
echo '</pre>';
}
?>
<?php
$arr05a = [1 => 33.33, 2 => 2.456, 5 => M_PI];
var_dump($arr05a);
echo '<br>';
setlocale(LC_ALL, "en_US", "en-US");
if(arsort($arr05a, SORT_LOCALE_STRING) == true)
{
var_dump($arr05a);
}
echo '<br><br><br>';
$arr05b = [1 => 33.33, 2 => 2.456, 5 => M_PI];
var_dump($arr05b);
echo '<br>';
setlocale(LC_ALL, "pt_BR.utf-8", "pt-BR");
if(arsort($arr05b, SORT_LOCALE_STRING) == true)
{
var_dump($arr05b);
}
?>
<?php
$arr06a = [ 1 => TRUE, 2 => FALSE, 3 => 1,
4 => 0, 5 => NULL, 6 => 1 ];
var_dump($arr06a);
echo '<br><br>';
if(arsort($arr06a, SORT_REGULAR) == true)
{
var_dump($arr06a);
}
?>
<?php
/*
* Testing arsort() by providing
* different octal array for $array argument
* with following flag values
* 1.flag value as default
* 2.SORT_REGULAR - compare items normally
* 3.SORT_NUMERIC - compare items numerically
*/
echo "Usage variations:<br><br>";
$unsorted_oct_array = [01235, 0321, 0345,
066, 0772, 077, -066, -0345, 0];
echo "(octal value array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_oct_array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(octal value array)<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $unsorted_oct_array;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(octal value array)<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $unsorted_oct_array;
var_dump(arsort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump($temp_array);
?>
<?php
/*
* Testing arsort() by providing
* mixed value array for $array argument
* with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
*/
echo "Usage variations:";
$mixed_values = array (
array(),
array(array(33, -5, 6), array(11), array(22, -55), array() ),
-4, "4", 4.00, "b", "5", -2, -2.0, -2.98989, "-.9", "True", "",
NULL, "ab", "abcd", 0.0, -0, "abcd\x00abcd\x00abcd", '', true, false
);
echo "<br><br>(mixed value array)<br>'flag' value is default:<br>";
$temp_array = $mixed_values;
var_dump(arsort($temp_array) );
echo '<br><br>';
var_dump($temp_array);
echo "<br><br>(mixed value array)<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $mixed_values;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br><br>';
var_dump($temp_array);
?>
<?php
/*
* Testing arsort() by providing
* different integer/float value arrays
* for $array argument
* with following flag values
* 1. flag value as default
* 2. SORT_REGULAR - compare items normally
* 3. SORT_NUMERIC - compare items numerically
* 4. SORT_STRING - compare items as strings
*/
$various_arrays = array (
array(11, -11, 21, -21, 31, -31, 0, 41, -41),
array(10.5, -10.5, 10.5e2, 10.6E-2, .5, .01, -.1),
array(.0001, .0021, -.01, -1, 0,
.09, 2, -.9, 10.6E-2, -10.6E-2, 33),
array(2147483647, 2147483648,
-2147483647, -2147483648, -0, 0, -2147483649)
);
$flag_value = array("SORT_REGULAR" => SORT_REGULAR,
"SORT_NUMERIC" => SORT_NUMERIC);
$count = 1;
echo "Various integer/float arrays:";
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag<br>";
$temp_array = $array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
foreach($flag_value as $key => $flag){
echo "<br><br>Sort flag = $key<br>";
$temp_array = $array;
var_dump(arsort($temp_array, $flag) );
echo '<br>';
var_dump($temp_array);
}
$count++;
}
?>
<?php
/*
* Testing arsort() by providing reference
* variable array with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
*/
$value1 = 100;
$value2 = 33;
$value3 = 555;
$unsorted_numerics = array( &$value1 , &$value2, &$value3);
echo "<br>(Reference variable array)<br>'flag' value is default:<br>";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(Reference variable array)<br>'flag' = SORT_REGULAR:<br>";
$temp_array = &$unsorted_numerics;
var_dump( arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(Reference variable array)<br>'flag' = SORT_NUMERIC:<br>";
$temp_array = &$unsorted_numerics;
var_dump( arsort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump( $temp_array);
?>
<?php
/*
* Testing arsort() by providing
* different string arrays for $array argument
* with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_STRING - compare items as strings
*/
$various_arrays = array (
array(null, NULL, "\a", "\cx", "\e", "\f",
"\n", "\r", "\t", "\xhh", "\ddd", "\v"),
array("lemoN", "Orange", "banana", "apple",
"Test", "TTTT", "ttt", "ww", "x",
"X", "oraNGe", "BANANA")
);
$flags = array("SORT_REGULAR" => SORT_REGULAR,
"SORT_STRING" => SORT_STRING);
$count = 1;
echo "Various string arrays:";
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag:<br>";
$temp_array = $array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
foreach($flags as $key => $flag){
echo "<br><br>Sort flag = $key<br>";
$temp_array = $array;
var_dump(arsort($temp_array, $flag) );
echo '<br>';
var_dump($temp_array);
}
$count++;
}
?>
<?php
/*
* Testing arsort() by providing different
* hexa-decimal array for $array argument
* with following flag values
* flag value as default
* SORT_REGULAR - compare items normally
* SORT_NUMERIC - compare items numerically
*/
$unsorted_hex_array = [0x1AB, 0xFFF, 0xF, 0xFF,
0x2AA, 0xBB, 0x1ab,
0xff, -0xFF, 0, -0x2aa];
echo "(hexadecimal value array)
<br>'flag' value is default:<br>";
$temp_array = $unsorted_hex_array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(hexadecimal value array)
<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $unsorted_hex_array;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(hexadecimal value array)
<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $unsorted_hex_array;
var_dump(arsort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump($temp_array);
?>
<?php
/*
* Testing arsort() by providing bool value array
* for $array argument with following flag values.
* flag value as default
* SORT_REGULAR - compare items normally
*/
$bool_values = [true, false, TRUE, FALSE];
echo "(bool value array)
<br>'flag' value is default:<br>";
$temp_array = $bool_values;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_REGULAR:<br>";
$temp_array = $bool_values;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_NUMERIC:<br>";
$temp_array = $bool_values;
var_dump(arsort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>(bool value array)
<br>'flag' value is SORT_STRING:<br>";
$temp_array = $bool_values;
var_dump(arsort($temp_array, SORT_STRING) );
echo '<br>';
var_dump($temp_array);
?>
<?php
/*
* Testing arsort() by providing arrays contains
* sub arrays for $array argument
* with flowing flag values
* flag value as default
* SORT_REGULAR - compare items normally
*/
$various_arrays = array (
array(),
array( array() ),
array(44, 11, array(64, 61) ),
array(array(33, -5, 6), array(11), array(22, -55), array() )
);
$count = 1;
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag:<br>";
$temp_array = $array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>Sort flag = SORT_REGULAR<br>";
$temp_array = $array;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
$count++;
}
?>
<?php
/*
* Testing arsort() by providing arrays
* with key values for $array argument
* with following flag values.
* 1.flag value as default
* 2.SORT_REGULAR - compare items normally
* To test the new keys for the
* elements in the sorted array.
*/
$various_arrays = array(
array(5 => 55, 6 => 66, 2 => 22, 3 => 33, 1 => 11),
array ("fruits" => array("a" => "orange",
"b" => "banana",
"c" => "apple"),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array("first", 5 => "second", "third")
),
array(1, 1, 8 => 1, 4 => 1, 19, 3 => 13),
array('bar' => 'baz', "foo" => 1),
array('a' => 1,'b' => array('e' => 2,
'f' => 3),'c' => array('g' => 4),'d' => 5),
);
$count = 1;
echo "Various arrays with key values:";
foreach ($various_arrays as $array) {
echo "<br><br>Iteration: $count<br>";
echo "<br>With Default sort flag<br>";
$temp_array = $array;
var_dump(arsort($temp_array) );
echo '<br>';
var_dump($temp_array);
echo "<br><br>Sort flag = SORT_REGULAR<br>";
$temp_array = $array;
var_dump(arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump($temp_array);
$count++;
}
?>
<?php
/*
* Testing arsort() by providing
* arrays with default keys and assoc 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 "Basic functionality:<br>";
$unsorted_strings = array(
"l" => "lemon", "o" => "orange",
"O" => "Orange", "O1" => "Orange1",
"o2" => "orange2", "O3" => "Orange3",
"o20" => "orange20", "b" => "banana",
);
$unsorted_numerics = array( 100, 33, 555, 22 );
echo "<br>(string array)
<br>'flag' value is default:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)
<br>'flag' value is default:<br>";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array)
<br>'flag' = SORT_REGULAR:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)
<br>'flag' = SORT_REGULAR:<br>";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array, SORT_REGULAR) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array)
<br>'flag' = SORT_STRING:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array, SORT_STRING) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(case insensitive)
<br>'flag' = SORT_STRING|SORT_FLAG_CASE:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array,
SORT_STRING|SORT_FLAG_CASE) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(string array, natural)
<br>'flag' = SORT_NATURAL:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array, SORT_NATURAL) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(natural, case insensitive)
<br>'flag' = SORT_NATURAL|SORT_FLAG_CASE:<br>";
$temp_array = $unsorted_strings;
var_dump( arsort($temp_array,
SORT_NATURAL|SORT_FLAG_CASE) );
echo '<br>';
var_dump( $temp_array);
echo "<br><br>(numeric array)
<br>'flag' = SORT_NUMERIC:<br>";
$temp_array = $unsorted_numerics;
var_dump( arsort($temp_array, SORT_NUMERIC) );
echo '<br>';
var_dump( $temp_array);
?>