توابع rsort() در PHP

مثال

عناصر آرایه‌ی $cars را به ترتیب الفبایی به صورت کاهشی مرتب می‌کند:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars);
?>

Run Instance

تعریف و کاربرد

توابع rsort() آرایه‌ی عددی را به ترتیب کاهشی مرتب می‌کند.

توضیحات:لطفاً از sort() این تابع آرایه‌ی عددی را به ترتیب تصاعدی مرتب می‌کند.

نحوه‌ی نوشتن

rsort(array,sortingtype);
پارامتر توضیح
array ضروری. آرایه‌ای که قرار است مرتب شود را تعیین می‌کند.
sortingtype

اختیاری. روش مقایسه‌ی عناصر/آیتم‌های آرایه را تعیین می‌کند. مقادیر ممکن:

  • 0 = SORT_REGULAR - پیش‌فرض. هر مورد را به ترتیب معمولی مرتب می‌کند (ASCII استاندارد، نوع تغییر نمی‌کند)
  • 1 = SORT_NUMERIC - به عنوان یک عدد با هر مورد برخورد می‌شود.
  • 2 = SORT_STRING - به عنوان یک رشته با هر مورد برخورد می‌شود.
  • 3 = SORT_LOCALE_STRING - Treat each item as a string, based on the current locale setting (which can be changed by setlocale()).
  • 4 = SORT_NATURAL - Treat each item as a string, using a natural sorting similar to natsort().
  • 5 = SORT_FLAG_CASE - Can combine (bitwise OR) SORT_STRING or SORT_NATURAL to sort strings without case sensitivity.

Description

The rsort() function sorts the elements of the array in reverse order by key value. It is basically the same as the function of arsort().

Note:This function is array Assigns new key names to the units. This will delete the original key names and not just reorder them.

Returns TRUE if successful, otherwise FALSE.

The optional second parameter contains additional sorting flags.

Technical Details

Return Value: TRUE on success. FALSE on failure
PHP Version: 4+

More Examples

Example 1

Sort the elements in the array $numbers in descending order by number:

<?php
$numbers = array(4, 6, 2, 22, 11);
rsort($numbers);
?>

Run Instance

Example 2

Compare the items as numbers and sort the elements in the array $cars in descending order:

<?php
$cars = array("Volvo", "BMW", "Toyota");
rsort($cars, SORT_NUMERIC);
?>

Run Instance