PHP Sorting Array

PHP Sorting Array


It is useful to sort related data which is stored in an array alphabetical or numerical order, descending or ascending.

Some Sorting Array functions:

  • sort () - sort arrays in ascending order
  • rsort() - sort arrays in descending order
  • asort() - sort associative arrays in ascending order, according to the value
  • ksort() - sort associative arrays in ascending order, according to the key
  • arsort() - sort associative arrays in descending order, according to the value
  • krsort() - sort associative arrays in descending order, according to the key

Print_r () function is a built-in function in PHP and is used to print or display information stored in a variable.

Some Example for Sorting array function:

Sort ():

Output: Array ([0] => Oil [1] => Spark Plugs [2] => Tires)

rsort () function is used for sorting the elements of the indexed array in descending order.

Output: Array ([0] => 10 [1] => 7 [2] => 4 [3] => 2.5 [4] => 2 [5] => 1)