Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

PHP array sorting


May 11, 2021 PHP


Table of contents


PHP array sorting

When we use PHP arrays, we sometimes need to sort PHP arrays, and this section describes several sorting methods for PHP arrays.

Elements in an array can be in descending or ascending order in alphabetical or numerical order.


PHP - Array sort function

In this chapter, we'll look at the following PHP array sorting functions one by one:

  • Sort() - Ascending arrays
  • rsort() - Descending arrays
  • asort() - Ascends the array according to the value of the associated array
  • ksort() - Ascending the array according to the keys of the associated array
  • arsort() - The array is arranged in descending order based on the value of the associated array
  • krsort() - Descends the array according to the key of the associated array

Sort() - Ascending arrays

The following example $cars elements in the array are arranged in alphabetical ascending order:

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
?>

Run an instance . . .

The following example $numbers elements in the array are arranged in numerical ascending order:

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
?>

Run an instance . . .


rsort() - Descending arrays

The following example $cars elements in the array are arranged in descending alphabetical order:

<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
?>

Run an instance . . .

The following example $numbers elements in the array in descending order of numbers:

<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
?>

Run an instance . . .


asort() - Ascends the array according to the value of the array

The following example ascends the associated array based on the value of the array:

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

Run an instance . . .


ksort() - Ascends the array according to its keys

The following example ascends the associated array according to the key of the array:

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

Run an instance . . .


arsort() - The array is descended according to the value of the array

The following example ranks the associated arrays in descending order based on the value of the array:

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

Run an instance . . .


krsort() - The array is descended according to the key of the array

The following example descends the associated array according to the key of the array:

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

Run an instance . . .


Complete PHP Array reference manual

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

This reference manual provides a brief description of each function and an example of its application!

That's all there is to it with a PHP array sort, and in the next section we'll cover PHP global variables!