PHP asort() Function

In this chapter you will learn:

  1. Definition for PHP asort() Function
  2. Note for PHP asort() Function
  3. Syntax for PHP asort() Function
  4. Parameter for PHP asort() Function
  5. Example - Sort an associative array in ascending order, according to the value:
  6. Example - asort() works by directly changing the value you pass in

Definition

The asort() function takes an array and sorts it by its values while preserving the keys.

Note

The asort() function is used to sort associative array. It is the associative array version of sort().

Syntax

PHP asort() Function has the following syntax.

bool arsort ( array &arr [, int options] )

Parameter

ParameterIs RequiredDescription
arrayRequired.Array to sort
sortingtypeOptional.How to compare the array elements/items.

Possible values for sortingtype:

ValueDescription
0 = SORT_REGULARDefault. Compare items normally (don't change types)
1 = SORT_NUMERICCompare items numerically
2 = SORT_STRINGCompare items as strings
3 = SORT_LOCALE_STRINGCompare items as strings, based on current locale
4 = SORT_NATURALCompare items as strings using natural ordering
5 = SORT_FLAG_CASEcan be combined bitwise OR with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

Example 1

Sort an associative array in ascending order, according to the value:


<?php/*from j a  v a2s.co m*/
    $age=array("PHP"=>"5","Java"=>"7","java2s.com"=>"9");
    asort($age);
    print_r($age);
?>

The code above generates the following result.

Example 2

asort() works by directly changing the value you pass in. The return value is either true or false, depending on whether the sorting was successful.


<?PHP/*j  a  v  a2 s. c o m*/
$capitalcities['Key1'] = 'Z';
$capitalcities['Key2'] = 'X';
$capitalcities['Key3'] = 'Y';
asort($capitalcities);
print_r($capitalcities);
?>

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. Definition for PHP compact() Function
  2. Syntax for PHP compact() Function
  3. Parameter for PHP compact() Function
  4. Example - Create an array with compact
  5. Example - Using a string that does not match a variable, and an array of variable names
Home » PHP Tutorial » PHP Array Functions
PHP array() function
PHP array_change_key_case() function
PHP array_chunk() function
PHP array_combine() function
PHP array_count_values() function
PHP array_diff() function
PHP array_diff_assoc() Function
PHP array_diff_key() function
PHP array_diff_uassoc() function
PHP array_diff_ukey() function
PHP array_fill() function
PHP array_fill_keys() function
PHP array_filter() function
PHP array_flip() function
PHP array_intersect() Function
PHP array_intersect_assoc() Function
PHP array_intersect_key() function
PHP array_intersect_uassoc() Function
PHP array_intersect_ukey() Function
PHP array_key_exists() Function
PHP array_keys() function
PHP array_map() Function
PHP array_merge() function
PHP array_merge_recursive() Function
PHP array_multisort() Function
PHP array_pad() Function
PHP array_pop() function
PHP array_product() Function
PHP array_push() function
PHP array_rand() function
PHP array_reduce() Function
PHP array_replace() Function
PHP array_replace_recursive() Function
PHP array_reverse() Function
PHP array_search() Function
PHP array_shift() Function
PHP array_slice() Function
PHP array_splice() Function
PHP array_sum() Function
PHP array_udiff() Function
PHP array_udiff_assoc() Function
PHP array_udiff_uassoc() Function
PHP array_uintersect() Function
PHP array_uintersect_assoc() Function
PHP array_uintersect_uassoc() Function
PHP array_unique() Function
PHP array_unshift() Function
PHP array_values() Function
PHP array_walk() Function
PHP array_walk_recursive() Function
PHP arsort() Function
PHP asort() Function
PHP compact() Function
PHP count() Function
PHP current() Function
PHP each() Function
PHP end() Function
PHP explode() Function
PHP extract() Function
PHP implode() Function
PHP in_array() Function
PHP key() Function
PHP krsort() Function
PHP ksort() Function
PHP list() Function
PHP natcasesort() Function
PHP natsort() Function
PHP next() Function
PHP pos() Function
PHP prev() Function
PHP range() Function
PHP reset() Function
PHP rsort() Function
PHP shuffle() Function
PHP sizeof() Function
PHP sort() Function
PHP uasort() Function
PHP uksort() Function
PHP usort() Function