Site icon

Sorting an Array in PHP

Sorting an Array in PHP

Today in this blog we will understand the concept of Sorting an Array in PHP. We will be covering the basic functions for arrays sorting in PHP.

Array sorting becomes much more important when it comes to complex programming. For example, you want to display the marks of a student in ascending order. Arrays can help you perform these functions with ease. With the help of arrays, you can sort the data in numerical, alphabetical, ascending and descending order as well.

Let’s start, first of all, we know about several sort functions for arrays in PHP.

There are several sort functions for arrays in PHP like sort(), rsort(), asort(), arsort(), ksort(), krsort(), natsort(), natcasesort(), shuffle(), usort(), uasort(), uksort().

Now, we will understand these sort function in details.
sort()
Sort an array in ascending order by value.
[pastacode manual=”%24fruits%20%3D%20%5B’Zitrone’%2C%20’Orange’%2C%20’Banane’%2C%20’Apfel’%5D%3B%0D%0Asort(%24fruits)%3B%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”php”/]
results in
[pastacode manual=”Array%20(%20%5B0%5D%20%3D%3E%20Apfel%20%5B1%5D%20%3D%3E%20Banane%20%5B2%5D%20%3D%3E%20Orange%20%5B3%5D%20%3D%3E%20Zitrone%20)” provider=”manual” lang=”default”/]
rsort()
Sort an array in descending order by value.
[pastacode manual=”%24fruits%20%3D%20%5B’Zitrone’%2C%20’Orange’%2C%20’Banane’%2C%20’Apfel’%5D%3B%20%0D%0Arsort(%24fruits)%3B%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B0%5D%20%3D%3E%20Zitrone%20%5B1%5D%20%3D%3E%20Orange%20%5B2%5D%20%3D%3E%20Banane%20%5B3%5D%20%3D%3E%20Apfel%20)” provider=”manual” lang=”default”/]
asort()
Sort an array in ascending order by value and preserve the indices.
[pastacode manual=”%24fruits%20%3D%20%5B1%20%3D%3E%20’lemon’%2C%202%20%3D%3E%20’orange’%2C%203%20%3D%3E%20’banana’%2C%204%20%3D%3E%20’apple’%5D%3B%20%0D%0Aasort(%24fruits)%3B%20%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B4%5D%20%3D%3E%20apple%20%5B3%5D%20%3D%3E%20banana%20%5B1%5D%20%3D%3E%20lemon%20%5B2%5D%20%3D%3E%20orange%20)” provider=”manual” lang=”default”/]
arsort()
Sort an array in descending order by value and preserve the indices.
[pastacode manual=”%24fruits%20%3D%20%5B1%20%3D%3E%20’lemon’%2C%202%20%3D%3E%20’orange’%2C%203%20%3D%3E%20’banana’%2C%204%20%3D%3E%20’apple’%5D%3B%20%0D%0Aarsort(%24fruits)%3B%20%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B2%5D%20%3D%3E%20orange%20%5B1%5D%20%3D%3E%20lemon%20%5B3%5D%20%3D%3E%20banana%20%5B4%5D%20%3D%3E%20apple%20)” provider=”manual” lang=”default”/]
ksort()
Sort an array in ascending order by key
[pastacode manual=”%24fruits%20%3D%20%5B’d’%3D%3E’lemon’%2C%20’a’%3D%3E’orange’%2C%20’b’%3D%3E’banana’%2C%20’c’%3D%3E’apple’%5D%3B%20%0D%0Aksort(%24fruits)%3B%20%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5Ba%5D%20%3D%3E%20orange%20%5Bb%5D%20%3D%3E%20banana%20%5Bc%5D%20%3D%3E%20apple%20%5Bd%5D%20%3D%3E%20lemon%20)” provider=”manual” lang=”default”/]
krsort()
Sort an array in descending order by key.
[pastacode manual=”%24fruits%20%3D%20%5B’d’%3D%3E’lemon’%2C%20’a’%3D%3E’orange’%2C%20’b’%3D%3E’banana’%2C%20’c’%3D%3E’apple’%5D%3B%20%0D%0Akrsort(%24fruits)%3B%20%0D%0Aprint_r(%24fruits)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5Bd%5D%20%3D%3E%20lemon%20%5Bc%5D%20%3D%3E%20apple%20%5Bb%5D%20%3D%3E%20banana%20%5Ba%5D%20%3D%3E%20orange%20)” provider=”manual” lang=”default”/]
natsort()
Sort an array in a way a human being would do (natural order).
[pastacode manual=”%24files%20%3D%20%5B’File8.stack’%2C%20’file77.stack’%2C%20’file7.stack’%2C%20’file13.stack’%2C%20’File2.stack’%5D%3B%20%0D%0Anatsort(%24files)%3B%20%0D%0Aprint_r(%24files)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B4%5D%20%3D%3E%20File2.stack%20%5B0%5D%20%3D%3E%20File8.stack%20%5B2%5D%20%3D%3E%20file7.stack%20%5B3%5D%20%3D%3E%20file13.stack%20%5B1%5D%20%3D%3E%20file77.stack%20)” provider=”manual” lang=”default”/]
natcasesort()
Sort an array in a way a human being would do (natural order), but case intensive.
[pastacode manual=”%24files%20%3D%20%5B’File8.stack’%2C%20’file77.stack’%2C%20’file7.stack’%2C%20’file13.stack’%2C%20’File2.stack’%5D%3B%20%0D%0Anatcasesort(%24files)%3B%20%0D%0Aprint_r(%24files)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B4%5D%20%3D%3E%20File2.stack%20%5B2%5D%20%3D%3E%20file7.stack%20%5B0%5D%20%3D%3E%20File8.stack%20%5B3%5D%20%3D%3E%20file13.stack%20%5B1%5D%20%3D%3E%20file77.stack%20)” provider=”manual” lang=”default”/]
shuffle()
Shuffles an array (sorted randomly).
[pastacode manual=”%24array%20%3D%20%5B’aa’%2C%20’bb’%2C%20’cc’%5D%3B%20%0D%0Ashuffle(%24array)%3B%0D%0Aprint_r(%24array)%3B” provider=”manual” lang=”default”/]
As written in the description it is random so here only one example in what it can result in
[pastacode manual=”Array%20(%20%5B0%5D%20%3D%3E%20cc%20%5B1%5D%20%3D%3E%20bb%20%5B2%5D%20%3D%3E%20aa%20)” provider=”manual” lang=”default”/]
usort()
Sort an array with a user defined comparison function.
[pastacode manual=”function%20compare(%24a%2C%20%24b)%20%7B%20%0D%0A%20%20if%20(%24a%20%3D%3D%20%24b)%20%7B%20return%200%3B%20%7D%20%0D%0A%20%20return%20(%24a%20%3C%20%24b)%20%3F%20-1%20%3A%201%3B%20%0D%0A%7D%0D%0A%24array%20%3D%20%5B3%2C%202%2C%205%2C%206%2C%201%5D%3B%20%0D%0Ausort(%24array%2C%20’compare’)%3B%20%0D%0Aprint_r(%24array)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5B0%5D%20%3D%3E%201%20%5B1%5D%20%3D%3E%202%20%5B2%5D%20%3D%3E%203%20%5B3%5D%20%3D%3E%205%20%5B4%5D%20%3D%3E%206%20)” provider=”manual” lang=”default”/]
You May Also Like:

uasort()
Sort an array with a user defined comparison function and preserve the keys.
[pastacode manual=”function%20compare(%24a%2C%20%24b)%20%7B%20%0D%0A%20%20if%20(%24a%20%3D%3D%20%24b)%20%7B%20return%200%3B%20%7D%20%0D%0A%20%20return%20(%24a%20%3C%20%24b)%20%3F%20-1%20%3A%201%3B%20%0D%0A%7D%0D%0A%24array%20%3D%20%5B’a’%20%3D%3E%201%2C%20’b’%20%3D%3E%20-3%2C%20’c’%20%3D%3E%205%2C%20’d’%20%3D%3E%203%2C%20’e’%20%3D%3E%20-5%5D%3B%20%0D%0Auasort(%24array%2C%20’compare’)%3B%20%0D%0Aprint_r(%24array)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5Be%5D%20%3D%3E%20-5%20%5Bb%5D%20%3D%3E%20-3%20%5Ba%5D%20%3D%3E%201%20%5Bd%5D%20%3D%3E%203%20%5Bc%5D%20%3D%3E%205%20)” provider=”manual” lang=”default”/]
uksort()
Sort an array by keys with a user defined comparison function.
[pastacode manual=”function%20compare(%24a%2C%20%24b)%20%7B%20%0D%0A%20%20if%20(%24a%20%3D%3D%20%24b)%20%7B%20return%200%3B%20%7D%20%0D%0A%20%20return%20(%24a%20%3C%20%24b)%20%3F%20-1%20%3A%201%3B%20%0D%0A%7D%0D%0A%24array%20%3D%20%5B’ee’%20%3D%3E%201%2C%20’g’%20%3D%3E%20-3%2C%20’4’%20%3D%3E%205%2C%20’k’%20%3D%3E%203%2C%20’oo’%20%3D%3E%20-5%5D%3B%0D%0Auksort(%24array%2C%20’compare’)%3B%20%0D%0Aprint_r(%24array)%3B” provider=”manual” lang=”default”/]
results in
[pastacode manual=”Array%20(%20%5Bee%5D%20%3D%3E%201%20%5Bg%5D%20%3D%3E%20-3%20%5Bk%5D%20%3D%3E%203%20%5Boo%5D%20%3D%3E%20-5%20%5B4%5D%20%3D%3E%205%20)” provider=”manual” lang=”default”/]
That’s all for Sorting an Array in PHP. With this, we come to an end to this tutorial and the Basics of PHP array series. I have tried my level best to cover everything basic about Sorting an array in PHP. If you have any doubts or question comment down below.

Thanks for Reading, Keep Visiting.

Exit mobile version