elibraryportal Logo

Sorting Arrays

Sorting Arrays sorted in alphabetical or numerical order, in descending or ascending order.

There are following Sort Functions Arrays that is.

  1. sort() :- sort function provide in ascending order
  2. rsort() :- sort function provide in descending order
  3. asort() :- according to the value, sort associative arrays in ascending order.
  4. ksort() :- according to the key, sort associative arrays in ascending order.
  5. arsort() :- according to the value , sort associative arrays in descending order.
  6. krsort():- according to the key , sort associative arrays in descending order.

1) sort() function in PHP

This function is used to sorting the elements of the indexed array in ascending order .

Example 1 :-

Output

Android
Java
PHP

Example 2 :-

Output

1
2
3
5
10


2) rsort() function in PHP

This function is used to sorting the elements of the indexed array in decending order .

Example 1 :-

Output

 PHP
Java
Android

Example 2 :-

Output

22
11
6
4
2

3) asort() function in PHP

According to the value, sorts an associative array in ascending order.

Example 1 :-

Output

 Key=Peter, Value=10
Key=Joe, Value=20
Key=Ben, Value=40


4) ksort() function in PHP

According to the key, sorts an associative array in ascending order.

Example :-

Output

 Key=Ben, Value=10
Key=Joe, Value=43
Key=Peter, Value=20

5) krsort()

According to the key , sorts an associative array in descending order.

Example :-

Output

 Key=Peter, Value=30
Key=Joe, Value=53
Key=Ben, Value=40


Next Topic