Open In App

Sort an Associative Array by Key in PHP

Last Updated : 04 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an Associative Array, the task is to sort the associative array by its keys in PHP.

There are different methods to sort Associative Array by keys, these are described below:

Using ksort() Function

The ksort() function sorts an associative array by its keys in ascending order. It sorts in a way that the relationship between the indices and values is maintained.

Example: This example shows the use of ksort() function.

PHP
<?php
$subjects = array(
    "Maths" => 95, 
    "Physics" => 90,   
    "Chemistry" => 96, 
    "English" => 93,   
    "Computer" => 98
);

ksort($subjects);

foreach ($subjects as $key => $value) {
    echo "$key => $value\n";
}
?>

Output
Chemistry => 96
Computer => 98
English => 93
Maths => 95
Physics => 90

Using uksort() Function

The uksort() function allows you to sort an associative array by keys using a custom comparison function.

Example: This example shows the use of uksort() function.

PHP
<?php

$subjects = array(
    "Maths" => 95, 
    "Physics" => 90,   
    "Chemistry" => 96, 
    "English" => 93,   
    "Computer" => 98
);

uksort($subjects, function($a, $b) {
    return strcmp($a, $b);
});

foreach ($subjects as $key => $value) {
    echo "$key => $value\n";
}

?>

Output
Chemistry => 96
Computer => 98
English => 93
Maths => 95
Physics => 90

Converting to a Regular Array for Sorting

You can convert the associative array to a regular array for sorting, then convert it back to an associative array. Converting the associative array to a regular array for sorting involves extracting the keys using array_keys(), sorting them using sort(), and then reconstructing the associative array using a loop.

Example: This example shows the sorting of an array by converting it to the regular array.

PHP
<?php
$subjects = array(
    "Maths" => 95, 
    "Physics" => 90,   
    "Chemistry" => 96, 
    "English" => 93,   
    "Computer" => 98
);

$keys = array_keys($subjects);

sort($keys);

$sorted_subjects = array();

foreach ($keys as $key) {
    $sorted_subjects[$key] = $subjects[$key];
}

foreach ($sorted_subjects as $key => $value) {
    echo "$key => $value\n";
}
?>

Output
Chemistry => 96
Computer => 98
English => 93
Maths => 95
Physics => 90

Using array_multisort() FunctionAdde

The array_multisort() function can be used to sort multiple arrays at once or to sort a multidimensional array by one or more dimensions. We can extract the keys of the associative array, sort them, and use array_multisort() to sort the array based on these sorted keys.

Example: This example demonstrates the use of the array_multisort() function.

PHP
<?php
$subjects = [
    "Maths" => 95,
    "Physics" => 90,
    "Chemistry" => 96,
    "English" => 93,
    "Computer" => 98,
];

$keys = array_keys($subjects);
$values = array_values($subjects);

array_multisort($keys, SORT_ASC, $values);
$sorted_subjects = array_combine($keys, $values);

foreach ($sorted_subjects as $key => $value) {
    echo "$key => $value\n";
}
?>

Output
Chemistry => 96
Computer => 98
English => 93
Maths => 95
Physics => 90

Next Article
Article Tags :

Similar Reads