PHP asort() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Array

Definition

In PHP, asort() sorts an array by its values from lowest to highest and keeps the keys attached to their values.

The "a" stands for associative, meaning the key-value pairs stay together. It is a good choice when the keys carry meaning, such as names or IDs.

Syntax

Call asort() with the values you need. Start with this line.

PHP
asort($scores);
CallWhat it does
asort($scores)Sorts by value from low to high, keeping keys.
asort($names, SORT_STRING | SORT_FLAG_CASE)Sorts text ignoring upper and lower case.
$ok = asort($data)Always returns true.

asort() sorts the array in place. For a list where keys do not matter, sort() is simpler. For high-to-low order, use arsort().

Library declarations
PHP
asort(array &$array, int $flags = SORT_REGULAR): true

Parameters

ParameterDescription
arrayRequired. The array to sort. It is passed by reference.
flagsOptional. Changes how values are compared: SORT_REGULAR (default), SORT_NUMERIC, SORT_STRING, SORT_LOCALE_STRING, SORT_NATURAL and SORT_FLAG_CASE.

Return Value

Return ValueDescription
trueAlways returns true. The array is sorted in place.

PHP Examples

Example 1 — Sort Scores Low to High

Order players from the lowest score to the highest.

PHP
<?php
$scores = ["Ann" => 80, "Ben" => 95, "Cara" => 70];
asort($scores);

foreach ($scores as $name => $score) {
    echo $name, ": ", $score, PHP_EOL;
}

How It Works

  1. The array stores a score for each name.
  2. asort() puts the values in ascending order.
  3. Each name moves together with its own score.
  4. The loop prints from the lowest score to the highest.

Example 2 — asort() vs sort()

asort() keeps the original keys, while sort() renumbers them.

PHP
<?php
$a = ["x", "a", "m"];
asort($a);
echo json_encode($a), PHP_EOL;

$b = ["x", "a", "m"];
sort($b);
echo json_encode($b), PHP_EOL;

How It Works

  1. Both arrays start with "x" at key 0, "a" at key 1 and "m" at key 2.
  2. asort() puts the values in order but each keeps its old key.
  3. The keys now read 1, 2, 0, so json_encode() writes an object.
  4. sort() assigns new keys 0, 1, 2, giving a plain list.

Example 3 — Case-Sensitive vs Case-Insensitive

Use flags to ignore case when sorting text.

PHP
<?php
$names = ["banana", "apple", "Cherry"];

$default = $names;
asort($default);
echo implode(", ", $default), PHP_EOL;

$ignoreCase = $names;
asort($ignoreCase, SORT_STRING | SORT_FLAG_CASE);
echo implode(", ", $ignoreCase), PHP_EOL;

How It Works

  1. By default PHP compares character codes, and uppercase "C" comes before lowercase letters.
  2. So "Cherry" is first in the default order.
  3. SORT_STRING | SORT_FLAG_CASE compares the text without caring about case.
  4. Now the order is alphabetical: apple, banana, Cherry.

Frequently Asked Questions

It sorts an array by value in ascending order while keeping the key-value pairs together.
asort() keeps the keys. sort() throws them away and renumbers from 0.
asort() sorts by value. ksort() sorts by key.
Use arsort().
No. It sorts the array in place and returns true.

Next: compact()

compact() creates an array from variables, using the variable names as keys.

compact() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful