PHP Sorting Arrays

In this tutorial, learn PHP sorting arrays using functions like sort(), rsort(), asort(), arsort(), ksort(), and krsort().

What is Sorting Arrays in PHP?

Sorting arrays is the process of arranging elements in ascending and descending. PHP provides functions to sort elements of indexed and associative arrays.

The indexed array may contain elements that can be numeric or string. Sorting of numeric elements arranged them in numeric ascending and descending. While sorting string elements arranged them in alphabetical ascending and descending orders.

Sorting of associative arrays including arranging elements according to the keys or values.

Let’s find out the functions that are useful for sorting arrays in PHP.

PHP Function to Sort Arrays in PHP

Here is the function for sorting of arrays in PHP:

  • sort(): Sorting Indexed Arrays in Ascending Order.
  • rsort(): Sorting Indexed Arrays in Descending Order.
  • asort(): Sorting Associative Arrays in Ascending Order by Value.
  • arsort(): Sorting Associative Arrays in Descending Order by Value.
  • ksort(): Sorting Associative Arrays in Ascending Order by Key.
  • krsort(): Sorting Associative Arrays in Descending Order by Key.

These functions can be used to arrange elements of indexed and associative arrays in PHP. Let’s learn each function and the method to sort arrays using them.

Sorting Indexed Arrays in Ascending Order

You can arrange the elements of an indexed array in ascending order using the sort() in PHP. The below example contains the alphabetical elements and the function arranges them in ascending alphabetically.

Output

Bike
Bolero
Car
Cycle

Similarly, you can sort the numerically indexed array element in ascending numerically using the sort() of PHP.

Output

0
1
3
7
9
13

Sorting Indexed Arrays in Descending Order

The indexed array can be arranged in descending order using the rsort() function in PHP. The below example contains the indexed array with alphabetical elements. The function arranges the elements in ascending order alphabetically.

Output

Cycle
Car
Bolero
Bike

Similarly, you can perform sorting over the numerically indexed array elements in descending order. It uses the same sort() function to arrange elements in descending order numerically in PHP.

Output

13
9
7
3
1
0

Sorting Associative Arrays in Ascending Order by Value

The asort() function arrange the elements of an associative array in ascending order by value. The below example contains the associative array that uses the function for sorting:

Output

Key is: Cycle, Value is: 2
Key is: Bike, Value is: 5
Key is: Car, Value is: 9

The above example contains the output that shows the associative array with ascending order according to the value. The example contains the value associated with its keys.

Sorting Associative Arrays in Descending Order by Value.

You can arrange the elements of an associative array in descending order using the arsort() in PHP. It sorts the elements in descending according to the value as given below:

Output

Key is: Car, Value is: 9
Key is: Bike, Value is: 5
Key is: Cycle, Value is: 2

The above example contains output that shows the descending ordered associative array according to the value.

Sorting Associative Arrays in Ascending Order by Key

The elements of an associative array can be arranged in ascending order using the ksort(). It sorts the elements in ascending order according to the key as given below:

Output

Key is: Bike, Value is: 5
Key is: Car, Value is: 9
Key is: Cycle, Value is: 2

The above example contains the output that shows the keys in ascending order according to its key.

Sorting Associative Arrays in Descending Order by Key

The ksort() function sort the elements of an associative array in descending order. It sorts the elements of an associative array in descending order according to the key as given below:

Output

Key is: Cycle, Value is: 2
Key is: Car, Value is: 9
Key is: Bike, Value is: 5

The above example shows the output that contains the keys in descending order according to its key.

You May Also Like to Read