ArrayObject::ksort


Sort the entries by key.



<?php

void 
public ArrayObject::ksort void )

?>

 ATTENTION 


This function sorts the entries by key, maintaining key to entry correlations.

This is used mainly when sorting associative arrays.



  1 EXERCISE   

<?php

$Dwarfs 
= [ => 'Bashful'=> 'Doc'
                   
=> 'Grumpy'=> 'Happy',
                   
=> 'Sneezy'=> 'Sleepy'=> 'Dopey'];

$dwArrayObject = new ArrayObject($Dwarfs);

$dwArrayObject->ksort();

foreach(
$dwArrayObject as $dw => $dwc)
{
echo 
'[ ' $dw ' ] - ';
var_dump($dw$dwc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 1 ] - int(1) string(7) "Bashful"

[ 2 ] - int(2) string(3) "Doc"

[ 3 ] - int(3) string(6) "Grumpy"

[ 4 ] - int(4) string(5) "Happy"

[ 5 ] - int(5) string(6) "Sneezy"

[ 6 ] - int(6) string(6) "Sleepy"

[ 7 ] - int(7) string(5) "Dopey"


  2 EXERCISE   

<?php

$Ellayers 
= [ 'K' => 2'L' => 8'M' => 18'N' => 32'O' => 32'P' => 18'Q' => 8];

$elArrayObject = new ArrayObject($Ellayers);

$elArrayObject->ksort();

foreach(
$elArrayObject as $el => $elc)
{
echo 
'[ ' $el ' ] - ';
var_dump($el$elc);
echo 
'<br><br>';
}

?>

 RESULT   

[ K ] - string(1) "K" int(2)

[ L ] - string(1) "L" int(8)

[ M ] - string(1) "M" int(18)

[ N ] - string(1) "N" int(32)"

[ O ] - string(1) "O" int(32)

[ P ] - string(1) "P" int(18)

[ Q ] - string(1) "Q" int(8)


  3 EXERCISE   

<?php

$Conts 
= [ 'c' => 299792458'G' => 6.67428E-11 ];  

$ctArrayObject = new ArrayObject($Conts);

$ctArrayObject->ksort();

foreach(
$ctArrayObject as $ct => $ctc)
{
echo 
'[ ' $ct ' ] - ';
var_dump($ct$ctc);
echo 
'<br><br>';
}

?>

 RESULT   

[ G ] - string(1) "G" float(6.67428E-11)

[ c ] - string(1) "c" int(299792458)


  4 EXERCISE   

<?php

$Cout_cont 
= [  "Countries" => [ 'BRA' => "Brasil"'POR' => "Portugal"'JPN' => "Japan" ], 
                        
"Continents" =>  ['SA' => "South America"'EU' => "Europe"'AS' => "Asia"]];  

$ccArrayObject = new ArrayObject($Cout_cont);

$ccArrayObject->ksort();

foreach(
$ccArrayObject as $cc => $ccc)
{
echo 
'[ ' $cc ' ] - ';
var_dump($cc$ccc);
echo 
'<br><br>';
}

?>

 RESULT   

[ Continents ] - string(10) "Continents" array(3) { ["SA"]=> string(13) "South America" ["EU"]=> string(6) "Europe" ["AS"]=> string(4) "Asia" }

[ Countries ] - string(9) "Countries" array(3) { ["BRA"]=> string(6) "Brasil" ["POR"]=> string(8) "Portugal" ["JPN"]=> string(5) "Japan" }


  5 EXERCISE   

<?php

$nmValues 
= [ 23413022, -120.234569.999 ];  

$nmArrayObject = new ArrayObject($nmValues);

$nmArrayObject->ksort();

foreach(
$nmArrayObject as $nm => $nmc)
{
echo 
'[ ' $nm ' ] - ';
var_dump($nm$nmc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) int(234)

[ 1 ] - int(1) int(130)

[ 2 ] - int(2) int(22)

[ 3 ] - int(3) int(-12)

[ 4 ] - int(4) float(0.23456)

[ 5 ] - int(5) float(9.999)


  6 EXERCISE   

<?php

$mvValues 
= [ 'two hundred thirty-four'234
                      
'One hundred and thirty'130
                      
'minus one hundred and twelve', -12
                      
'Nine point nine hundred and ninety nine'9.999 ];  

$mvArrayObject = new ArrayObject($mvValues);

$mvArrayObject->ksort();

foreach(
$mvArrayObject as $mv => $mvc)
{
echo 
'[ ' $mv ' ] - ';
var_dump($mv$mvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) string(23) "two hundred thirty-four"

[ 1 ] - int(1) int(234)

[ 2 ] - int(2) string(22) "One hundred and thirty"

[ 3 ] - int(3) int(130)

[ 4 ] - int(4) string(28) "minus one hundred and twelve"

[ 5 ] - int(5) int(-12)

[ 6 ] - int(6) string(39) "Nine point nine hundred and ninety nine"

[ 7 ] - int(7) float(9.999)


  7 EXERCISE   

<?php

$nvValues 
= [ => 'two hundred thirty-four'=> 234
                      
=> 'One hundred and thirty'=> 130
                      
=> 'minus one hundred and twelve'=> -12
                      
=> 'Nine point nine hundred and ninety nine'=> 9.999 ];  

$nvArrayObject = new ArrayObject($nvValues);

$nvArrayObject->ksort();

foreach(
$nvArrayObject as $nv => $nvc)
{
echo 
'[ ' $nv ' ] - ';
var_dump($nv$nvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 1 ] - int(1) string(23) "two hundred thirty-four"

[ 2 ] - int(2) string(22) "One hundred and thirty")

[ 3 ] - int(3) int(234)

[ 4 ] - int(4) string(28) "minus one hundred and twelve"

[ 5 ] - int(5) int(130)

[ 6 ] - int(6) float(9.999)

[ 7 ] - int(7) int(-12)

[ 8 ] - int(8) string(39) "Nine point nine hundred and ninety nine"


  8 EXERCISE   

<?php

$ovValues 
= [ 123=> 'x'=> 'X'=> 'b'=> 'c'=> 'A' ];  

$ovArrayObject = new ArrayObject($ovValues);

$ovArrayObject->ksort();

foreach(
$ovArrayObject as $ov => $ovc)
{
echo 
'[ ' $ov ' ] - ';
var_dump($ov$ovc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) int(1)

[ 1 ] - int(1) int(2)

[ 2 ] - int(2) int(3)

[ 3 ] - int(3) string(1) "x"

[ 4 ] - int(4) string(1) "b"

[ 5 ] - int(5) string(1) "c"

[ 6 ] - int(6) string(1) "X"

[ 7 ] - int(7) string(1) "A"


  9 EXERCISE   

<?php

$otValues 
= [ 'one''two''three'];

$pvValues = [ 123, ...$otValues ];  

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

        The Unpacking inside arrays: 
              ...$otValues, 
        was introduced in PHP 7.4.0 

   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

$pvArrayObject = new ArrayObject($pvValues);

$pvArrayObject->ksort();

foreach(
$pvArrayObject as $pv => $pvc)
{
echo 
'[ ' $pv ' ] - ';
var_dump($pv$pvc);
echo 
'<br><br>';
}

?>

 RESULT   

[ 0 ] - int(0) int(1)

[ 1 ] - int(1) int(2)

[ 2 ] - int(2) int(3)

[ 3 ] - int(3) string(3) "one"

[ 4 ] - int(4) string(3) "two"

[ 5 ] - int(5) string(5) "three"