w3resource

PHP Array Exercises : Sort an associative array


8. Sort Associative Array by Key and Value

Write a PHP script to sort the following associative array :

array("Sophia"=>"31","Jacob"=>"41","William"=>"39","Ramesh"=>"40") in
a) ascending order sort by value
b) ascending order sort by Key
c) descending order sorting by Value
d) descending order sorting by Key

Sample Solution:

PHP Code:

<?php
// Echo a message indicating the start of the section for sorting associative arrays in ascending order by value
echo "
Associative array : Ascending order sort by value";

// Define an associative array and sort it in ascending order by value using asort()
$array2 = array("Sophia" => "31", "Jacob" => "41", "William" => "39", "Ramesh" => "40");
asort($array2);

// Iterate through the sorted array and echo the key-value pairs
foreach ($array2 as $y => $y_value) {
    echo "Age of " . $y . " is : " . $y_value . "";
}

// Echo a message indicating the start of the section for sorting associative arrays in ascending order by key
echo "
Associative array : Ascending order sort by Key";

// Define an associative array and sort it in ascending order by key using ksort()
$array3 = array("Sophia" => "31", "Jacob" => "41", "William" => "39", "Ramesh" => "40");
ksort($array3);

// Iterate through the sorted array and echo the key-value pairs
foreach ($array3 as $y => $y_value) {
    echo "Age of " . $y . " is : " . $y_value . "";
}

// Echo a message indicating the start of the section for sorting associative arrays in descending order by value
echo "
Associative array : Descending order sorting by Value";

// Define an associative array and sort it in descending order by value using arsort()
$age = array("Sophia" => "31", "Jacob" => "41", "William" => "39", "Ramesh" => "40");
arsort($age);

// Iterate through the sorted array and echo the key-value pairs
foreach ($age as $y => $y_value) {
    echo "Age of " . $y . " is : " . $y_value . "";
}

// Echo a message indicating the start of the section for sorting associative arrays in descending order by key
echo "
Associative array : Descending order sorting by Key";

// Define an associative array and sort it in descending order by key using krsort()
$array4 = array("Sophia" => "31", "Jacob" => "41", "William" => "39", "Ramesh" => "40");
krsort($array4);

// Iterate through the sorted array and echo the key-value pairs
foreach ($array4 as $y => $y_value) {
    echo "Age of " . $y . " is : " . $y_value . "";
} 
?>

Output:

Associative array : Ascending order sort by value           
Age of Sophia is : 31                                       
Age of William is : 39                                      
Age of Ramesh is : 40                                       
Age of Jacob is : 41                                        
                                                            
Associative array : Ascending order sort by Key             
Age of Jacob is : 41                                        
Age of Ramesh is : 40                                       
Age of Sophia is : 31                                       
Age of William is : 39                                      
                                                            
Associative array : Descending order sorting by Value       
Age of Jacob is : 41                                        
Age of Ramesh is : 40                                       
Age of William is : 39                                      
Age of Sophia is : 31                                       
                                                            
Associative array : Descending order sorting by Key         
Age of William is : 39                                      
Age of Sophia is : 31                                       
Age of Ramesh is : 40  
Age of Jacob is : 41 

Flowchart:

Flowchart: Sort an  associative array

For more Practice: Solve these Related Problems:

  • Write a PHP script to sort an associative array in ascending order by value and then by key, outputting both sorted arrays.
  • Write a PHP function to sort a given associative array in descending order by key and then in descending order by value using appropriate sorting functions.
  • Write a PHP program to sort an associative array in multiple ways (ascending by value, ascending by key, descending by value, descending by key) and display each result.
  • Write a PHP script that implements custom sort logic for an associative array based on a combined key-value criterion.

Go to:


PREV : Insert New Array Item by Position.
NEXT : Average Temperature & Extremes.

PHP Code Editor:



Contribute your code and comments through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.