PHP: arsort() function
PHP: Sort an array in reverse order
The arsort() function is used to sort an array in reverse order. The function maintains index association.
This function is used mainly when sorting associative arrays where the actual element order is significant.
Version:
(PHP 4 and above)
Syntax:
arsort(array_name, sort_type)
Parameters:
Name | Description | Required / Optional |
Type |
---|---|---|---|
array_name | Specifies the name of the array to sort. | Required | Array |
sort_type | Sets the sorting behavior. Possible type : SORT_REGULAR - Compare items normally. SORT_NUMERIC - Compare items numerically. SORT_STRING - Compare items as strings. SORT_LOCALE_STRING - compare items as strings, based on the current locale |
Optional | Integer |
Return value:
TRUE on success or FALSE on failure.
Value Type: Array
Example:
<?php
$subject = array('d' => 'Language', 'c' => 'Math', 'a' => 'Science', 'b'=> 'Geography');
arsort($subject);
foreach ($subject as $key=>$val)
{
echo "$key = $val <br />";
}
?>
Output:
a = Science c = Math d = Language b = Geography
View the example in the browser
Practice here online:
See also
Previous: array_walk
Next: arsort
PHP: Tips of the Day
Filters the collection using the given callback
Example:
<?php function tips_reject($items, $func) { return array_values(array_diff($items, array_filter($items, $func))); } print_r(tips_reject(['Apple', 'Pear', 'Kiwi', 'Banana'], function ($item) { return strlen($item) > 4; })); ?>
Output:
Array ( [0] => Pear [1] => Kiwi )
- Weekly Trends
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- JavaScript String Exercises
- JavaScript HTML Form Validation
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
- JavaScript functions Exercises
- Python Tutorial
- Python Array Exercises
- SQL Cross Join
- C# Sharp Array Exercises