Content · Programming · PHP

PHP Collator::asort() Method: Usage and Examples

Understand Collator::asort() in PHP with syntax, working code examples, expected output, practical use cases, and common mistakes.

1 views Published Oct 16, 2026 Updated Oct 16, 2026

When writing PHP applications, you may need to sort multilingual string values using locale-aware rules while preserving keys. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.

The Collator::asort() operation provides this behavior directly. The examples below show what happens to the inputs and what to check before using the result in your application.

What Is Collator::asort() in PHP?

Sort multilingual string values using locale-aware rules while preserving keys.

Its syntax is:

$collator->asort(array &$array, int $flags = Collator::SORT_REGULAR): bool

Parameters used by this operation:

  • &$array — The collection being examined or updated.
  • $flags — An optional flag that controls comparison or processing behavior.

Requires the intl extension. Exact collation follows ICU and locale settings; avoid treating byte order as a language-aware alphabet.

Example 1: Basic Usage

Start with a small, self-contained input so you can inspect exactly how the operation behaves.

<?php

$collator = new Collator("de_DE");
$names = ["first" => "Özil", "second" => "Anton", "third" => "Zebra"];
$collator->asort($names);
print_r($names);

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Example 2: Another Important Case

This variation highlights another input shape, a comparison option, or a change in the collection.

<?php

$collator = new Collator("en_US");
$names = ["b" => "banana", "a" => "Apple", "c" => "cherry"];
$collator->asort($names);
print_r($names);

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Practical Example: Sort user-facing names according to the language selected for a directory

Sort user-facing names according to the language selected for a directory. Save the code in a PHP file and adapt the sample data to your project.

<?php

$collator = new Collator("fr_FR");
$cityNames = ["first" => "Évreux", "second" => "Angers", "third" => "Lyon"];
$collator->asort($cityNames);
foreach ($cityNames as $cityCode => $cityName) { echo "$cityCode: $cityName" . PHP_EOL; }

Output: Depends on the request environment, installed extension, selected locale, or filesystem state.

Important Behavior and Edge Cases

Requires the intl extension. Exact collation follows ICU and locale settings; avoid treating byte order as a language-aware alphabet.

  • Test the operation with empty or missing input and decide how your application should respond.
  • Read the return value independently from any changes to variables passed by reference.
  • Validate untrusted input before using the result for permissions, database changes, or user-visible output.

Comparison With Related Functions

Function or ApproachWhen to Use It
Collator::asort()Sort multilingual string values using locale-aware rules while preserving keys
Related functionsCollator::sort() renumbers keys; asort() keeps them.

Common Beginner Mistakes

MistakeCorrect Approach
Confusing key sorting with value sortingCheck whether the comparison applies to keys or values before selecting the sort method.
Expecting an unchanged collectionThe sorting operation updates the array or collection in place; copy it first when you need the original.
Ignoring comparison rulesRequires the intl extension. Exact collation follows ICU and locale settings; avoid treating byte order as a language-aware alphabet.

Key Takeaways

  • Collator::asort() helps you sort multilingual string values using locale-aware rules while preserving keys.
  • Requires the intl extension. Exact collation follows ICU and locale settings; avoid treating byte order as a language-aware alphabet.
  • Collator::sort() renumbers keys; asort() keeps them.
  • Use the practical example as a starting point, but validate real application inputs.

Collator::asort() is useful when you need to sort multilingual string values using locale-aware rules while preserving keys. Use its documented rules and the examples above to choose the right behavior for your data.

Share this resourceSend this page to someone who may find it useful.