Skip to content

PHP

How do we sort arrays in PHP?

Learn how PHP’s sort, rsort, asort, arsort, ksort, and krsort functions order indexed and associative arrays by values or keys.

3 min read

I wanted to write a series of articles about the solutions to the questions that I came across in my research about frequently asked questions in PHP Developer interviews and that I liked.

One of these questions was How do we sort arrays in PHP?. There are a number of functions that PHP has built-in to sort arrays in PHP. These are;

  • sort() - Sorts arrays in ascending order.
  • rsort() - Sorts arrays in descending order.
  • asort() - Sorts the arrays in the array in ascending order by their value.
  • ksort() - Sorts the arrays in the array ascending based on their key values.
  • arsort() - Sorts the arrays in the array in descending order of their values.
  • krsort() - Sorts the arrays in the array in descending order of key values.

can be sorted. Let's illustrate them with applications.

Sorting indexed arrays

Sorting Arrays in Ascending Order - sort()

It sorts the arrays in ascending order, for example;

$array = [1,52,69,9,32,13,45,102];

sort($array);
// Accordingly, when we print the array on the screen, we get an output as follows.

print_r($array);
// Output: Array ( [0] => 1 [1] => 9 [2] => 13 [3] => 32 [4] => 45 [5] => 52 [6] => 69 [7 ] => 102)

Sorting Arrays in Arrays by Values

Sort Ascending - asort()

This function is used to sort other arrays in an array according to their values in ascending order. For example;

$arrayception = [
     'Cobb' => 34,
     'Arthur' => 25,
     'goods' => 2,
     'Eames' => 13,
     'Saito' => 8
];

asort($arrayception);

print_r($arrayception);
// When we print this array on the screen, it will output as follows;
// Output: Array ( [Mal] => 2 [Saito] => 8 [Eames] => 13 [Arthur] => 25 [Cobb] => 34 )

Sort Descending - arsort()

This function is used to sort other arrays in the array in descending order of their values. For example;

$arrayception = [
     'Cobb' => 34,
     'Arthur' => 25,
     'goods' => 2,
     'Eames' => 13,
     'Saito' => 8
];

arsort($arrayception);

print_r($arrayception);
// When we print this array on the screen, it will output as follows;
// Output: Array ( [Cobb] => 34 [Arthur] => 25 [Eames] => 13 [Saito] => 8 [Mal] => 2 )

Sorting Arrays in Array by Key Value

Ascending Sort - ksort()

This function is used to sort other arrays in ascending order according to their key values. For example;

$arrayception = [
     'Cobb' => 34,
     'Arthur' => 25,
     'goods' => 2,
     'Eames' => 13,
     'Saito' => 8
];

ksort($arrayception);

print_r($arrayception);
// When we print this array on the screen, it will output as follows;
// Output: Array ( [Arthur] => 25 [Cobb] => 34 [Eames] => 13 [Mal] => 2 [Saito] => 8 )

Descending Sort - krsort()

Finally, the krsort() function is used to sort other arrays in descending order of their key values. For example;

$arrayception = [
     'Cobb' => 34,
     'Arthur' => 25,
     'goods' => 2,
     'Eames' => 13,
     'Saito' => 8
];

krsort($arrayception);

print_r($arrayception);
// When we print this array on the screen, it will output as follows;
// Output: Array ( [Saito] => 8 [Mal] => 2 [Eames] => 13 [Cobb] => 34 [Arthur] => 25 )

Share this article

Vue6 min read

Authentication with Firebase and VueJS

Build Firebase authentication in Vue with Pinia, GitHub login, session-aware route middleware, and protected authenticated and guest pages.

  • Vue
  • Firebase
  • Authentication
Vue2 min read

Pinia vs Vuex

Compare Pinia and Vuex through simple state management examples, API differences, and practical guidance for choosing a store for your Vue application.

  • Vue
  • Pinia
  • Vuex