PHP Array Sort By Key Value Example

Apr 05, 2021 . Admin

Hello Friends,

In this blog Now let's see how to various PHP array sorting functions First, we have the sort() method used to array sort PHP code in an ascending order. For a descending order, utilize rsort.

There are four functions for associative arrays — you either array sort PHP by key or by value.

To PHP sort array by key, you should utilize ksort() (for ascending order) or krsort() (for descending order). To PHP sort array by value, you will require functions asort() and arsort() (for ascending and descending orders).

Types for Sorting Function...

Example : sort() PHP sort() function sorts an array in an ascending order. Now Let's see example of sort() function.
<?php

      $name = ['Ramesh', 'Suresh', 'Jayesh'];
      sort($name);
      print_r($name);

?>
Output
Array
(
    [0] => Jayesh
    [1] => Ramesh
    [2] => Suresh
)

Let's see another example This time the array holds numbers and sorts them in numerical order:

<?php

    $num = [10, 50, 20, 40, -1];
    sort($num);
    print_r($num);

?>
Output
Array
(
    [0] => -1
    [1] => 10
    [2] => 20
    [3] => 40
    [4] => 50
)
Example : rsort()

PHP rsort() function sorts an array in an descending order. Now Let's see example of rsort() function.

<?php

      $name = ['Ramesh', 'Suresh', 'Jayesh'];
      rsort($name);
      print_r($name);

?>
Output
Array
(
    [0] => Suresh
    [1] => Ramesh
    [2] => Jayesh
)
Let's do that with numbers. You will descry the script engenders an opposite result than sort() did in the antecedent example:
<?php

    $num = [10, 50, 20, 40, -1];
    rsort($num);
    print_r($num);

?>
Output
Array
(
    [0] => 50
    [1] => 40
    [2] => 20
    [3] => 10
    [4] => -1
)
Example : asort() and arsort()

asort() and arsort() are used to PHP sort associative arrays by their value. Now Let's see We use asort() for the ascending order:

<?php

      $marks = [
        'eng' => 80, 
        'maths' => 88,
        'sci' => 78
      ];    
      asort($marks);
      print_r($marks);

?>
Output
Array
(
    [sci] => 78
    [eng] => 80
    [maths] => 88
)

When we require our array sorted by key in an descending order, we call arsort():

<?php

 $marks = [
    'eng' => 80, 
    'maths' => 88,
    'sci' => 78
  ];    
  arsort($marks);
  print_r($marks);

?>
Output
Array
(
    [maths] => 88
    [eng] => 80
    [sci] => 78
)
Example : ksort() and krsort()

ksort() and krsort() make PHP sort associative arrays, but they don't do it by value: what matters here is the key. In our example, designations were the keys. Hence, utilizing these two functions will sort out guys not by weight but by their names (alphabetically).

Now, Let's see example of ksort().
<?php

      $marks = [
        'eng' => 80, 
        'maths' => 88,
        'sci' => 78
      ];    
      ksort($marks);
      print_r($marks);

?>
Output
Array
(
    [eng] => 80
    [maths] => 88
    [sci] => 78
)

If descending order sounds more acceptable, we choose krsort():

<?php

 $marks = [
    'eng' => 80, 
    'maths' => 88,
    'sci' => 78
  ];    
  krsort($marks);
  print_r($marks);

?>
Output
Array
(
    [sci] => 78
    [maths] => 88
    [eng] => 80
)

It will help you..

#PHP