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: Table of ContentUsing ksort() FunctionUsing uksort() FunctionConverting to a Regular Array for SortingUsing array_multisort() FunctionAddeUsing ksort() FunctionThe 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"; } ?> OutputChemistry => 96 Computer => 98 English => 93 Maths => 95 Physics => 90 Using uksort() FunctionThe 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"; } ?> OutputChemistry => 96 Computer => 98 English => 93 Maths => 95 Physics => 90 Converting to a Regular Array for SortingYou 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"; } ?> OutputChemistry => 96 Computer => 98 English => 93 Maths => 95 Physics => 90 Using array_multisort() FunctionAddeThe 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"; } ?> OutputChemistry => 96 Computer => 98 English => 93 Maths => 95 Physics => 90 Comment More infoAdvertise with us Next Article Sort an Associative Array by Key in PHP B blalverma92 Follow Improve Article Tags : PHP Similar Reads Associative Arrays in PHP An associative array in PHP is a special array where each item has a name or label instead of just a number. Usually, arrays use numbers to find things. For example, the first item is at position 0, the second is 1, and so on. But in an associative array, we use words or names to find things. These 4 min read Sort an array of dates in PHP We are given an array of multiple dates in (Y-m-d) format. We have to write a program in PHP to sort all the dates in the array in decreasing order. Examples : Input : array("2018-06-04", "2014-06-08", "2018-06-05") Output : 2018-06-05 2018-06-04 2014-06-08 Input : array("2016-09-12", "2009-09-08", 2 min read How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte 3 min read ArrayObject uasort() Function in PHP The uasort() function of the ArrayObject class in PHP is used to sort values of an ArrayObject according to a user defined comparison function. The function compares and arranges the values present in the ArrayObject according to the given comparison function. This method does not affects the key-va 2 min read ArrayObject uksort() Function in PHP The uksort() function of the ArrayObject class in PHP is used to sort the entries present in the ArrayObject according to the keys following a user-defined function. They key-value mapping is preserved after sorting the ArrayObject. Syntax: void uksort($comparator) Parameters: This function accepts 2 min read PHP array_keys() Function The array_keys() is a built-in function in PHP and is used to return either all the keys of and array or the subset of the keys. Syntax: array array_keys($input_array, $search_value, $strict) Parameters: The function takes three parameters out of which one is mandatory and other two are optional. $i 2 min read How to use array_merge() and array_combine() in PHP ? In this article, we will discuss about how to use array_merge() and array_combine() functions in PHP. Both functions are array based functions used to combine two or more arrays using PHP. We will see each function with syntax and implementation array_merge() FunctionThis function merges the two or 3 min read PHP array_âkey_âfirst() Function The array_âkey_âfirst() function is an inbuilt function in PHP that is used to get the first key of an array. This function returns the first key without affecting the internal array pointer. Syntax: int|string|null array_key_first(array $array) Parameters: This function accepts a single parameter $ 1 min read PHP array_uintersect_assoc() Function The array_uintersect_assoc() function is an inbuilt function in PHP used to compute the intersection of the array of keys for different values of two or more arrays. The initial array or first array compare to all others array by the callback function or user-defined function and returns the matches 4 min read How to Sort JSON Object Arrays Based on a Key? Sorting is the process of arranging elements in a specific order. In JavaScript, we can sort a JSON array by key using various methods, such as the sort() function. Along with this, we can use various other approaches and methods that are implemented in this article. Below are the approaches to sort 3 min read Like