Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

PHP Array Sorting

PHP Array Sorting


Elements in an array can be arranged in descending or ascending order, alphabetically or numerically.


PHP - Array Sorting Functions

In this chapter, we will introduce the following PHP array sorting functions one by one:


sort() - Sort an array in ascending order

The example below arranges the elements of the $cars array in alphabetical ascending order:

Example

Run Example »

The example below arranges the elements of the $numbers array in numerical ascending order:

Example

Run Example »


rsort() - Sort an array in descending order

The example below arranges the elements of the $cars array in alphabetical descending order:

Example

Run Example »

The example below arranges the elements of the $numbers array in numerical descending order:

Example

Run Example »


asort() - Sort an array in ascending order by value

The example below sorts an associative array in ascending order by value:

Example

"35","Ben"=>"37","Joe"=>"43"); asort($age); ?>

Run Example »


ksort() - Sort an array in ascending order by key

The example below sorts an associative array in ascending order by key:

Example

"35","Ben"=>"37","Joe"=>"43"); ksort($age); ?>

Run Example »


arsort() - Sort an array in descending order by value

The example below sorts an associative array in descending order by value:

Example

"35","Ben"=>"37","Joe"=>"43"); arsort($age); ?>

Run Example »


krsort() - Sort an array in descending order by key

The example below sorts an associative array in descending order by key:

Example

"35","Ben"=>"37","Joe"=>"43"); krsort($age); ?>

Run Example »


Complete PHP Array Reference Manual

To view the complete reference manual for all array functions, please visit our PHP Array Reference Manual.

This reference manual provides a brief description and application example for each function!