Upgrade & Secure Your Future with DevOps, SRE, DevSecOps, MLOps!

We spend hours on Instagram and YouTube and waste money on coffee and fast food, but won’t spend 30 minutes a day learning skills to boost our careers.
Master in DevOps, SRE, DevSecOps & MLOps!

Learn from Guru Rajesh Kumar and double your salary in just one year.


Get Started Now!

How can I sort arrays and data in PHP?

PHP Functions For Sorting Arrays

In the previous chapter you’ve learnt the essentials of PHP arrays i.e. what arrays are, how to create them, how to view their structure, how to access their elements etc. You can do even more things with arrays like sorting the elements in any order you like.

PHP comes with a number of built-in functions designed specifically for sorting array elements in different ways like alphabetically or numerically in ascending or descending order. Here we’ll explore some of these functions most commonly used for sorting arrays.

  • sort() and rsort() — For sorting indexed arrays
  • asort() and arsort() — For sorting associative arrays by value
  • ksort() and krsort() — For sorting associative arrays by key

Sorting Indexed Arrays in Ascending Order

The sort() function is used for sorting the elements of the indexed array in ascending order (alphabetically for letters and numerically for numbers).

Example:

This print_r() statement gives the following output:

Array ( [0] => Blue [1] => Green [2] => Red [3] => Yellow )

Similarly you can sort the numeric elements of the array in ascending order.

Example:-

This print_r() statement gives the following output:

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

Sorting Indexed Arrays in Descending Order

The rsort() function is used for sorting the elements of the indexed array in descending order (alphabetically for letters and numerically for numbers).

Example:-

This print_r() statement gives the following output:

Array ( [0] => Yellow [1] => Red [2] => Green [3] => Blue )

Similarly, you can sort the numeric elements of the array in descending order.

Example

This print_r() statement gives the following output:

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

Sorting Associative Arrays in Ascending Order By Value

The asort() function sorts the elements of an associative array in ascending order according to the value. It works just like sort(), but it preserves the association between keys and its values while sorting.

Example:-

This print_r() statement gives the following output:

Array ( [Harry] => 14 [Peter] => 20 [Clark] => 35 [John] => 45 )

Sorting Associative Arrays in Descending Order By Value

The arsort() function sorts the elements of an associative array in descending order according to the value. It works just like rsort(), but it preserves the association between keys and its values while sorting.

Example:-

This print_r() statement gives the following output:

Array ( [John] => 45 [Clark] => 35 [Peter] => 20 [Harry] => 14 )

Sorting Associative Arrays in Ascending Order By Key

The ksort() function sorts the elements of an associative array in ascending order by their keys. It preserves the association between keys and its values while sorting, same as asort() function.

Example:-

This print_r() statement gives the following output:

Array ( [Clark] => 35 [Harry] => 14 [John] => 45 [Peter] => 20 )

Sorting Associative Arrays in Descending Order By Key

The krsort() function sorts the elements of an associative array in descending order by their keys. It preserves the association between keys and its values while sorting, same as arsort() function.

Example:-

This print_r() statement gives the following output:

Array ( [Peter] => 20 [John] => 45 [Harry] => 14 [Clark] => 35 )

Certification Courses

DevOpsSchool has introduced a series of professional certification courses designed to enhance your skills and expertise in cutting-edge technologies and methodologies. Whether you are aiming to excel in development, security, or operations, these certifications provide a comprehensive learning experience. Explore the following programs:

DevOps Certification, SRE Certification, and DevSecOps Certification by DevOpsSchool

Explore our DevOps Certification, SRE Certification, and DevSecOps Certification programs at DevOpsSchool. Gain the expertise needed to excel in your career with hands-on training and globally recognized certifications.