ArrayObject::asort


Sort the entries by value.



<?php

void 
public ArrayObject::asort void )

?>

 ATTENTION 


This function sorts the entries such that the keys maintain their correlation with the entries they are associated with.

This is used mainly when sorting associative arrays where the actual element order is significant.



  1 EXERCISE   

<?php

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

$dwArrayObject = new ArrayObject($Dwarfs);

$dwArrayObject->asort();

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"

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

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

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

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

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


  2 EXERCISE   

<?php

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

$elArrayObject = new ArrayObject($Ellayers);

$elArrayObject->asort();

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)

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

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

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

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

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


  3 EXERCISE   

<?php

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

$ctArrayObject = new ArrayObject($Conts);

$ctArrayObject->asort();

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->asort();

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->asort();

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

?>

 RESULT   

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

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

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

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

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

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


  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->asort();

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

?>

 RESULT   

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

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

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

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

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

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

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

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


  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->asort();

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

?>

 RESULT   

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

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

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

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

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

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

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

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


  8 EXERCISE   

<?php

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

$ovArrayObject = new ArrayObject($ovValues);

$ovArrayObject->asort();

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

?>

 RESULT   

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

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

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

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

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

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

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

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


  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->asort();

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

?>

 RESULT   

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

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

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

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

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

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