مرتبسازی آرایههای PHP
- Previous Page آرایههای PHP
- Next Page PHP Superglobals
عناصر آرایه میتوانند به ترتیب الفبا یا عددی به ترتیب افزایشی یا کاهشی مرتب شوند.
PHP - توابع مرتبسازی آرایه
در این بخش، ما در مورد توابع مرتبسازی آرایههای PHP زیر یاد خواهیم گرفت:
- sort() - ترتیب افزایشی آرایه
- rsort() - ترتیب کاهشی آرایه
- asort() - ترتیب افزایشی آرایههای مرتبط بر اساس مقدار
- ksort() - ترتیب افزایشی آرایههای مرتبط بر اساس کلید
- arsort() - ترتیب کاهشی آرایههای مرتبط بر اساس مقدار
- krsort() - ترتیب کاهشی آرایههای مرتبط بر اساس کلید
ترتیب افزایشی آرایه - sort()
مثال زیر به ترتیب الفبا به ترتیب افزایشی عناصر آرایه $cars را مرتب میکند:
Example
<?php $cars=array("porsche","BMW","Volvo"); sort($cars); ?>
The following example sorts the elements of the array $numbers in ascending order by number:
Example
<?php $numbers=array(3,5,1,22,11); sort($numbers); ?>
Sort the array in descending order - rsort()
The following example sorts the elements of the array $cars in descending order by letter:
Example
<?php $cars=array("porsche","BMW","Volvo"); rsort($cars); ?>
The following example sorts the elements of the array $numbers in descending order by number:
Example
<?php $numbers=array(3,5,1,22,11); rsort($numbers); ?>
Sort the array by value in ascending order - asort()
The following example sorts the associated array in ascending order by value:
Example
<?php $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); asort($age); ?>
Sort the array by key in ascending order - ksort()
The following example sorts the associated array in ascending order by key:
Example
<?php $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); ksort($age); ?>
Sort the array by value in descending order - arsort()
The following example sorts the associated array in descending order by value:
Example
<?php $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); arsort($age); ?>
Sort the array by key in descending order - krsort()
The following example sorts the associated array in descending order by key:
Example
<?php $age=array("Bill"=>"63","Steve"=>"56","Elon"=>"47"); krsort($age); ?>
Complete PHP Array Reference Manual
For a complete reference manual of array functions, please visit our PHP Array Reference Manual.
This reference manual includes a brief description and usage examples of each function.
- Previous Page آرایههای PHP
- Next Page PHP Superglobals