VOOZH about

URL: https://www.geeksforgeeks.org/php/how-to-sort-an-array-of-associative-arrays-by-value-of-a-given-key-in-php/

⇱ How to sort an Array of Associative Arrays by Value of a Given Key in PHP ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to sort an Array of Associative Arrays by Value of a Given Key in PHP ?

Last Updated : 23 Jul, 2025

Each entry in the associative array is characterized by a unique key-value pair. An array can contain singular data types belonging to variables or other arrays as its elements. There are multiple ways to sort an array of associative arrays by the value of a specified key. 

Approach 1: Using the array_multisort() method

The array_multisort() method is used to return a sorted array. String keys will be maintained, but the numeric keys are re-indexed, and they start at 0 and increase by 1. This function can sort multiple arrays at once or a multidimensional array. 

array_multisort(array, sort_order, sort_type);

Example: In this example, initially an array of associative arrays is defined. Then, a new array is created in order to store the keys as the attribute of the main array upon which we wish to sort. The array_multisort() method is then applied to this created array and the desired sort type. In case two or more keys are the same, the values appear in the order of storage. 

Output:

Modified Array : Array
(
[0] => Array
(
[Name] => AMAN
[marks] => 55
)

[1] => Array
(
[Name] => ANjali
[marks] => 98
)

[2] => Array
(
[Name] => ASHIKA
[marks] => 67
)

[3] => Array
(
[Name] => BASHIKA
[marks] => 87
)

[4] => Array
(
[Name] => YASHIKA
[marks] => 22
)

[5] => Array
(
[Name] => YASHIKA
[marks] => 100
)

[6] => Array
(
[Name] => YASHITA
[marks] => 24
)
)

Explanation: The main array is sorted based on names in ascending order. 

Approach 2: Using the usort() method

This method sorts the given array using a user-defined comparison function. The user-defined function should return an integer <, =, or > than 0 if the first argument is <, =, or > than the second argument. This method assigns new keys to the elements in the array. It just removes any existing keys that may have been assigned, rather than just simply reordering the keys.

usort(array, user_def_func)

Example: This example uses the usort() method to sort an Array of Associative Arrays by the Value of a Given Key in PHP.

Output: The DescSort() method sorts the marks in descending order. 

Modified Array : Array
(
[0] => Array
(
[Name] => YASHIKA
[marks] => 100
)

[1] => Array
(
[Name] => ANjali
[marks] => 98
)

[2] => Array
(
[Name] => BASHIKA
[marks] => 87
)

[3] => Array
(
[Name] => ASHIKA
[marks] => 67
)

[4] => Array
(
[Name] => AMAN
[marks] => 55
)

[5] => Array
(
[Name] => YASHITA
[marks] => 24
)

[6] => Array
(
[Name] => YASHIKA
[marks] => 22
)
)

Approach 3: Using the arsort() function

The arsort() in PHP is used to sort an array according to values. It sorts in a way that the relationship between indices and values is maintained. By default it sorts in descending order of values.

bool arsort( $array, $sorting_type )

Example: This example describes the use of the asort() function in order to sort the Array of Associative Arrays by Value for the Given Key in PHP.

Output:

Modified Array : Array ( [6] => 100 
[5] => 98
[2] => 87
[1] => 67
[4] => 55
[3] => 24
[0] => 22 )

Approach 4: Using array_walk() to Create a Keyed Array and ksort()

This approach uses array_walk() to create an array keyed by the values of the specified key, and then uses ksort() to sort the array by these keys. Finally, it reconstructs the original array order based on the sorted keys.

Example:


Output
Modified Array : Array
(
 [0] => Array
 (
 [Name] => NIKUNJ
 [marks] => 22
 )

 [1] => Array
 (
 [Name] => ARTI
 [marks] => 24
 ...

Approach 5: Using array_column() and array_multisort()

The array_column() function returns the values from a single column in the input array, identified by the column key. This approach first extracts the column of values by which you want to sort, then uses array_multisort() to sort the main array based on these values.

Example: Sorting by 'marks'


Output
Modified Array : Array
(
 [0] => Array
 (
 [Name] => YASHIKA
 [marks] => 100
 )

 [1] => Array
 (
 [Name] => ANjali
 [marks] => 9...
Comment