PHP complete se...LOGIN
PHP complete self-study manual
author:php.cn  update time:2022-04-15 13:53:54

PHP array sorting



The elements in the array can be arranged in descending or ascending alphabetical or numerical order.


PHP - Array sorting function

What are the methods for sorting arrays in php?

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

  • ##sort() - Sort the array in ascending order

  • rsort() - Sort an array in descending order

  • asort() - Sort an array in ascending order based on the values ​​of an associative array

  • ksort() - Sort the array in ascending order according to the keys of the associative array

  • arsort() - Sort the array in descending order based on the values ​​of the associative array

  • krsort() - Sort the array in descending order based on the keys of the associative array


sort() - Sort the array in ascending order

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

Example

<?php
$cars=array("Volvo","BMW","Toyota");
sort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
  echo $cars[$x];
  echo "<br />";
}
?>
Run the example»Click the "Run Example" button to view the online example

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

Example

<?php
$numbers=array(4,6,2,22,11);
sort($numbers);
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
   echo $numbers[$x];
   echo "<br />";
}
?>
Run instance»Click the "Run instance" button to view the online instance


rsort() - Sort the array in descending order

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

Example

<?php
$cars=array("Volvo","BMW","Toyota");
rsort($cars);
$clength=count($cars);
for($x=0;$x<$clength;$x++)
{
  echo $cars[$x];
  echo "<br />";
}
?>
Run the example»Click "Run Example" button to view online examples

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

Example

<?php
$numbers=array(4,6,2,22,11);
rsort($numbers);
$arrlength=count($numbers);
for($x=0;$x<$arrlength;$x++)
{
echo $numbers[$x];
echo "<br />";
}
?>
Run instance»Click the "Run instance" button to view the online instance


asort() - Sort the array in ascending order according to the value of the array

The following example sorts the associative array in ascending order according to the value of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
asort($age);
foreach($age as $x=>$x_value)
{
   echo "Key=" . $x . ", Value=" . $x_value;
   echo "<br />";
}
?>
Run Example»Click the "Run Example" button to view the online example


ksort() - Sort the array in ascending order according to the key of the array

The following example is based on the key of the array , sort the associative array in ascending order:

Instance

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
ksort($age);
foreach($age as $x=>$x_value)
{
   echo "Key=" . $x . ", Value=" . $x_value;
   echo "<br />";
}
?>
Run Instance»Click the "Run Instance" button to view the online instance


arsort() - Sort the array in descending order according to the value of the array

The following example sorts the associative array in descending order according to the value of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
arsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br />";
}
?>
Run Example»

Click the "Run Example" button to view the online example


krsort() - According to the array Sort the array in descending order according to the key of the array

The following example sorts the associated array in descending order according to the key of the array:

Example

<?php
$age=array("Peter"=>"35","Ben"=>"37","Joe"=>"43");
krsort($age);
foreach($age as $x=>$x_value)
{
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br />";
}
?>
Run Example»

Click the "Run Example" button to view the online example


Complete PHP Array Reference Manual

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

This reference manual provides a brief description and application examples of each function!