How to use arsort in PHP

In this article, I will explain how the arsort() function can be used in PHP.
  • 1828

arsort() function in PHP

  • The arsort() function is used to sort an array by the values in reverse order. The values keep their original keys.

  • The arsort() function returns TRUE on success, or FALSE on failure.

  • The arsort() function maintains index association.

Syntax

arsort(array,sorttype)

Parameter

  • array array is required parameter. the specified array whose values are to be counted.
  • sorttype sorttype is optional parameter. it determine, how to sort the array values. Possible values of sorttype is
    • SORT_REGULAR - this is the default case value. it don't change types.
    • SORT_NUMERIC - it's treat values numerically.
    • SORT_STRING - it's treat values as strings.
    • SORT_LOCALE_STRING - it's treat values as strings, based on local settings.

Example

The following example show to how the arsort() function can be used in PHP.

<html>
<body>
<h3 style="color: violet;">arsort() function example in PHP</h3>
   <?php
   $item = array('d' => 'Computer','c' => 'Mouse','a' => 'keyboard','b'=> 'Pendrive');
   arsort($item);
   foreach ($item as $key=>$val)
   {
   echo "$key = $val <br />";
   }
   ?>
</body>
</html>

 

Output

arsortphp.jpg
You may also want to read these related articles here

Ask Your Question

Got a programming related question? You may want to post your question here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.