PHP Sort Array

PHP Sort Array

In this tutorial, we will try to learn the sorting an array in PHP.

The sort() Function

The sort function sorts the arrays in ascending order (e.g; 1,2,3,4,…).

Output

C
DB
OOP

The arsort() Function

The arsort function sorts the associative arrays in descending order (e.g; 4,3,2,1), according to the value.

Output

OOP
DB
C

The ksort() Function

The ksort function sorts the associative arrays in ascending order (e.g; 1,2,3,4,…), according to the key.

The rsort() Function

The rsort function sorts the arrays in descending order (e.g; 4,3,2,1).

Output

OOP
DB
C

The asort()

The asort function sorts the associative arrays in ascending order (e.g; 1,2,3,4,…).

Output

Key=Asad, Value=5
Key=Ali, Value=22
Key=Akram, Value=55
Key=Sameed, Value=90

Attributes of Sorting Functions

Some attributes of sorting the functions are mentioned below.

ksort() Maintains key association Sorting with Key
natcasesort() Maintains key association Sorting with Values
asort() Maintains key association Sorting with Values
sort() Does not Maintains key association Sorting with Values
uasort() Maintains key association Sorting with Values
uksort() Maintains key association Sorting with Key
krsort() Maintains key association Sorting with Key
natsort() Maintains key association Sorting with Values
rsort() Does not Maintains key association Sorting with Values
shuffle() Does not Maintains key association Sorting with Values
usort() Does not Maintains key association Sorting with Values
array_multisort() associative Maintains key association, numeric Does not Maintain key association Sorting with Values
arsort() Maintains key association Sorting with Values

Sort an array in PHP

Sorting means to arrange the array values in an ascending or descending order. Here, we are sharing the code of Sort an array in PHP.

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP using Functions

Here, we are sharing the code of Sort an array in PHP using functions.

Output

The array
25 10 58 62 58 11 27 26 35 87
The sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP with While Loop

Here, we are sharing the code of Sort an array in PHP with while loop.

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an array in PHP with While Loop using functions

Here, we are sharing the code of Sort an array in PHP with while loop using functions.

Output

The First array
25 10 58 62 58
The second array
11 27 26 35 87
The union of the sorted array
10 11 25 26 27 35 58 58 62 87

Sort an Array in PHP with Form Values Entered By The User

Here, we are sharing the code of Sort an array in PHP with the form values entered by the user.

Output
How to Sort an array in PHP
Figure: How to Sort an array in PHP

Sort an Array in PHP with Form Values Entered By The User using functions

Here, we are sharing the code of Sort an array in PHP with the form values entered by the user using functions.

Words sorting in lexicographical order

Here, we are showing you the code of words in lexicographical order.

Output

Words
a b d e k q f z m c
Entered words in lexicographical order
a b c d e f k m q z

Words sorting in lexicographical order using the form

Here, we are showing you the code of words in lexicographical order using form.

Output

sort words in lexicographical order PHP
Figure: sort words in lexicographical order PHP

Words sorting in lexicographical order using the while loop

Here, we are showing you the code of words in lexicographical order using while loop.

Output

Words
a b d e k q f z m c
Entered words in lexicographical order
a b c d e f k m q z

Topic Covered

How to Sort an array in PHP.

Leave a Reply

Your email address will not be published. Required fields are marked *