How do i sort a multi dimensional array in php?

This solution is for usort() with an easy-to-remember notation for multidimensional sorting. The spaceship operator <=> is used, which is available from PHP 7.

usort($in,function($a,$b){
  return $a['first']   <=> $b['first']  //first asc
      ?: $a['second']  <=> $b['second'] //second asc
      ?: $b['third']   <=> $a['third']  //third desc (a b swapped!)
      //etc
  ;
});

Examples:

$in = [
  ['firstname' => 'Anton', 'surname' => 'Gruber', 'birthdate' => '03.08.1967', 'rank' => 3],
  ['firstname' => 'Anna', 'surname' => 'Egger', 'birthdate' => '04.01.1960', 'rank' => 1],
  ['firstname' => 'Paul', 'surname' => 'Mueller', 'birthdate' => '15.10.1971', 'rank' => 2],
  ['firstname' => 'Marie', 'surname' => 'Schmidt ', 'birthdate' => '24.12.1963', 'rank' => 2],
  ['firstname' => 'Emma', 'surname' => 'Mueller', 'birthdate' => '23.11.1969', 'rank' => 2],
];

First task: Order By rank asc, surname asc

usort($in,function($a,$b){
  return $a['rank']      <=> $b['rank']     //first asc
      ?: $a['surname']   <=> $b['surname']  //second asc
  ;
});

Second task: Order By rank desc, surname asc, firstmame asc

usort($in,function($a,$b){
  return $b['rank']      <=> $a['rank']       //first desc
      ?: $a['surname']   <=> $b['surname']    //second asc
      ?: $a['firstname'] <=> $b['firstname']  //third asc
  ;
});

Third task: Order By rank desc, birthdate asc

The date cannot be sorted in this notation. It is converted with strtotime.

usort($in,function($a,$b){
  return $b['rank']      <=> $a['rank']       //first desc
      ?: strtotime($a['birthdate']) <=> strtotime($b['birthdate'])    //second asc
  ;
});

Definition

This page explains how to sort a multidimensional array in PHP. This frequently happens when the array is the result of a database query.

The example array

Let's considere the following multidimensional array:

$array[] = array('name' => 'Dupont', 'age' => 72);
$array[] = array('name' => 'Albert', 'age' => 11);
$array[] = array('name' => 'Durand', 'age' => 56);
$array[] = array('name' => 'Michel', 'age' => 52);
$array[] = array('name' => 'Dupont', 'age' => 36);
$array[] = array('name' => 'Plutot', 'age' => 27);
NameAge
Dupont 72
Albert 11
Durand 56
Michel 52
Dupont 36
Plutot 27

Sort by name ascending

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

$columns = array_column($array, 'name');
array_multisort($columns, SORT_ASC, $array);

Here is the result:

NameAge
Albert 11
Dupont 36
Dupont 72
Durand 56
Michel 52
Plutot 27

Sort by age descending

$columns = array_column($array, 'age');
array_multisort($columns, SORT_DESC, $array);

Here is the result:

NameAge
Dupont 72
Durand 56
Michel 52
Dupont 36
Plutot 27
Albert 11

Sort by name ascending, then by age descending

$columns_1 = array_column($array, 'name');
$columns_2 = array_column($array, 'age');
array_multisort($columns_1, SORT_ASC, $columns_2, SORT_DESC, $array);

Here is the result:

NameAge
Albert 11
Dupont 72
Dupont 36
Durand 56
Michel 52
Plutot 27

See also

  • Add icons on your web pages
  • CSS color table
  • CSS styles for Rainbow code highlighter
  • How to detect ad blockers on web sites?
  • HTML drop-down list of citizenships.
  • HTML five stars ranking
  • How Bézier curves are described in SVG paths
  • How to check if a number is prime in JavaScript
  • How to get list of subdirectories in PHP
  • How to slugify a string in JavaScript?
  • How to slugify a string in PHP?
  • In PHP, how to convert a date in French?
  • Levenshtein distance in mySQL
  • List of supported languages by Prism syntax highlighter
  • Nice HTML checkbox
  • PHP array of countries and citizenships.
  • Pure CSS loader
  • Replace HTML checkbox with images
  • Simple SSE in PHP
  • Sessions or cookies?

Last update : 11/25/2021

How do i sort a multi dimensional array in php?

How do I sort a multidimensional array in PHP?

The array_multisort() function returns a sorted array. You can assign one or more arrays. The function sorts the first array, and the other arrays follow, then, if two or more values are the same, it sorts the next array, and so on.

Can we sort multidimensional array?

In a 2D array, a cell has two indexes one is its row number, and the other is its column number. Sorting is a technique for arranging elements in a 2D array in a specific order. The 2D array can be sorted in either ascending or descending order.

How do you sort a multidimensional list?

Use sorted() with a lambda function to sort a multidimensional list by column. Call sorted(iterable, key=None) with key set to a lambda function of syntax lambda x: x[i] to sort a multidimensional list iterable by the i th element in each inner list x .

How do you sort a multidimensional array by an inner key?

Sorting a multidimensional array by element containing date. Use the usort() function to sort the array. The usort() function is PHP builtin function that sorts a given array using user-defined comparison function. This function assigns new integral keys starting from zero to array elements.