<?php
bool krsort ( 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(krsort($arr01s) == true)
{
echo '<pre>';
var_dump($arr01s);
echo '</pre>';
}
?>
<?php
$arr02s = ["R" => 'red', "G" => 'green', "B" => 'blue',
"H" => 'HUE', "S" => 'SATURATION', "K" => 'BLACK'];
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
if(krsort($arr02s, SORT_STRING) == true)
{
echo '<pre>';
var_dump($arr02s);
echo '</pre>';
}
?>
<?php
$arr03s = ["São Paulo" => 'SP', "Santo André" => 'SP',
"Maceió" => 'AL'];
echo 'The given ARRAY:<br><pre>';
var_dump($arr03s);
echo '</pre>';
setlocale(LC_ALL, 'pt_BR', 'pt-BR');
if(krsort($arr03s, SORT_LOCALE_STRING) == true)
{
echo 'The resulting ARRAY:<br><pre>';
var_dump($arr03s);
echo '</pre>';
}
?>
<?php
$arr04s = [ 'PI' => M_PI, 'PHP_INT_MIN' => PHP_INT_MIN ];
echo 'The given ARRAY:<br><pre>';
var_dump($arr04s);
echo '</pre>';
setlocale(LC_ALL, 'pt_BR', 'pt-BR');
if(krsort($arr04s, SORT_LOCALE_STRING) == true)
{
echo 'The resulting ARRAY:<br><pre>';
var_dump($arr04s);
echo '</pre>';
}
?>
<?php
/*
* Testing krsort() 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(krsort($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(krsort($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(krsort($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(krsort($temp_array, SORT_STRING) );
echo '<br>';
var_dump($temp_array);
?>