krsort


php128 apg

SORT an ARRAY by key in reverse order.





This function returns TRUE on success or FALSE on failure.

This function is used mainly when sorting associative arrays.

This function sorts an ARRAY by key, maintaining key to data correlations



<?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 )

?> 

&$array


The input ARRAY.



 $sort_flags 


May be used to modify the sorting behavior.

It can be one of the following values:

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


  1 EXERCISE   

<?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>';
}

?> 

  2 EXERCISE   

<?php

$arr02s 
= ["R" => 'red'"G" => 'green'"B" => 'blue'
                 
"H" => 'HUE'"S" => 'SATURATION'"K" => 'BLACK'];

    echo 
'<pre>';
var_dump($arr02s);
    echo 
'</pre>';

if(
krsort($arr02sSORT_STRING) == true)
{
    echo 
'<pre>';
var_dump($arr02s);
    echo 
'</pre>';
}

?> 

  3 EXERCISE   

<?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($arr03sSORT_LOCALE_STRING) == true)
{
    echo 
'The resulting ARRAY:<br><pre>';
var_dump($arr03s);
    echo 
'</pre>';
}

?> 

  4 EXERCISE   

<?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($arr04sSORT_LOCALE_STRING) == true)
{
    echo 
'The resulting ARRAY:<br><pre>';
var_dump($arr04s);
    echo 
'</pre>';
}

?> 

  5 EXERCISE   

<?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 = [truefalseTRUEFALSE];

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_arraySORT_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_arraySORT_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_arraySORT_STRING) );
echo 
'<br>';
var_dump($temp_array);

?>