Sorting Arrays in PHP:

If you want to sort array by number or alphabet, or in ascending and descending order so in PHP you have functions to sort arrays.

Here is a list of some common functions that are used to sort an array.

  • sort()
  • rsort()
  • asort()
  • ksort()
  • arsort()
  • krsort()

 sort() Function:

If you want to sort an array by alphabetical or in a numeric ascending order than use sort() function. We will take different examples for alphabet and number.

Example:

            Sorting array in ascending alphabetical order.

Example:

            Sorting array in ascending numeric order.

 rsort() Function:

If you want to sort an array by alphabetical or in a numeric descending order than use rsort() function. We will take different examples for alphabet and number.

Example:

            Sorting array in descending alphabetical order.

 Example:

            Sorting array in descending numeric order.

 asort() Function:

With the help of asort() function you can sort an associative array in ascending order according to there values.

Example:

ksort() Function:

With the help of ksort() function you can sort an associative array in ascending order according to there keys.

Example:

 arsort() Function:

With the help of arsort() function you can sort an associative array in descending order according to there values.

Example:

 krsort() Function:

With the help of krsort() function you can sort an associative array in descending order according to there keys.

Example:

 

Leave a Reply

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