Previous | Home | Next |
array_asort()
asort() function sorts an associative array in ascending order, according to the value.
Syntax:asort(array,sortingtype);
In above syntax, ""array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:
- 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
- 1 = SORT_NUMERIC - Compare items numerically
- 2 = SORT_STRING - Compare items as strings
- 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
- 4 = SORT_NATURAL - Compare items as strings using natural ordering
- 5 = SORT_FLAG_CASE -
Example:
<?php $salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000"); asort($salary); foreach($salary as $x=>$x_value) { echo "Emp_Name=" . $x . ", Salary=" . $x_value; echo "<br>"; } ?>
Output:
Emp_Name=Roy, Salary=17000 Emp_Name=Smith, Salary=23000 Emp_Name=John, Salary=25000
array_arsort()
The arsort() function sorts an associative array in descending order, according to the value.
Syntax:arsort(array,sortingtype);
In above syntax, ""array" specifies the array to sort and "sortingtype" specifies how to compare the array elements/items by using values are as follows:
- 0 = SORT_REGULAR - Default. Compare items normally (don't change types)
- 1 = SORT_NUMERIC - Compare items numerically
- 2 = SORT_STRING - Compare items as strings
- 3 = SORT_LOCALE_STRING - Compare items as strings, based on current locale
- 4 = SORT_NATURAL - Compare items as strings using natural ordering
- 5 = SORT_FLAG_CASE -
Example:
<?php $salary=array("John"=>"25000","Roy"=>"17000","Smith"=>"23000"); arsort($salary); foreach($salary as $x=>$x_value) { echo "Emp_Name=" . $x . ", Salary=" . $x_value; echo "<br>"; } ?>
Output:
Emp_Name=John, Salary=25000 Emp_Name=Smith, Salary=23000 Emp_Name=Roy, Salary=17000
Previous | Home | Next |