PHP Programing language

array_sort() and array_rsort() Function in PHP
Previous Home Next

array_sort()

The sort() function sorts an indexed array in ascending order.

Syntax:
sort(array,sortingtype);

In above syntax, ""array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

 

Example:

<?php
$fruits=array("Apple","Mango","Banana");
sort($fruits);

$flength=count($fruits);
for($x=0;$x<$flength;$x++)
   {
   echo $fruits[$x];
   echo "<br>";
   }
?>

Output:

Apple
Banana
Mango

array_rsort()

The rsort() function sorts an indexed array in descending order.

Syntax:
rsort(array,sortingtype);

In above syntax, ""array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:

  • 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
  • 1 = SORT_NUMERIC - Compare items numerically
  • 2 = SORT_STRING - Compare items as strings
  • 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
  • 4 = SORT_NATURAL - Compare items as strings using natural ordering
  • 5 = SORT_FLAG_CASE -

Example:

<?php
$fruits=array("Apple","Mango","Banana");
rsort($fruits);

$flength=count($fruits);
for($x=0;$x<$flength;$x++)
   {
   echo $fruits[$x];
   echo "<br>";
   }
?>

Output:

Mango
Banana
Apple

Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: