How to use asort in PHP
In this article, I will explain how the asort() function can be used in PHP.
asort() function in PHP
- The asort() function is used to sort an array by the values in reverse order. The values keep their original keys.
- The asort() function returns TRUE on success, or FALSE on failure.
- The asort() function maintains index association.
Syntax
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 asort() function can be used in PHP.
<html>
<body>
<h3 style="color: violet;">asort() function example in PHP</h3>
<?php
$sub = array('d' => 'Hindi','c' => 'Math','a' => 'Science','b'=> 'English');
asort($sub);
foreach ($sub as $key=>$val)
{
echo "$key = $val <br />";
}
?>
</body>
</html>
|
Output
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