When writing PHP applications, you may need to sort values ascending without losing their original keys. This is useful in everyday tasks such as working with imported data, application settings, user-facing lists, and reports.
The 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 asort() in PHP?
Sort values ascending without losing their original keys.
Its syntax is:
asort(array &$array, int $flags = SORT_REGULAR): true
Parameters used by this operation:
&$array— The collection being examined or updated.$flags— An optional flag that controls comparison or processing behavior.
The input is sorted in place. Values determine the ordering; keys stay attached to their values.
Example 1: Basic Usage
Start with a small, self-contained input so you can inspect exactly how the operation behaves.
<?php
$prices = ["pen" => 4, "book" => 15, "eraser" => 2];
asort($prices, SORT_NUMERIC);
print_r($prices);
Output:
Array
(
[eraser] => 2
[pen] => 4
[book] => 15
)
The output shows the result of the first call. Check the printed keys and values as well as their order.
Example 2: Another Important Case
This variation highlights another input shape, a comparison option, or a change in the collection.
<?php
$names = ["a" => "Maya", "b" => "Aaron", "c" => "Zoe"];
asort($names, SORT_STRING);
print_r($names);
Output:
Array
(
[b] => Aaron
[a] => Maya
[c] => Zoe
)
Compare this result with Example 1: the difference comes from the input or option selected, not from additional processing.
Practical Example: Display a product catalog sorted by price while retaining product codes
Display a product catalog sorted by price while retaining product codes. Save the code in a PHP file and adapt the sample data to your project.
<?php
$shippingCosts = ["express" => 18, "standard" => 8, "pickup" => 0];
asort($shippingCosts, SORT_NUMERIC);
foreach ($shippingCosts as $method => $cost) { echo "$method: $cost" . PHP_EOL; }
Output:
pickup: 0
standard: 8
express: 18
The output comes from the completed workflow shown above. When adapting it, change the source records and keep the same checks for missing, unexpected, or invalid values.
Important Behavior and Edge Cases
The input is sorted in place. Values determine the ordering; keys stay attached to their values.
- 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 Approach | When to Use It |
|---|---|
asort() | Sort values ascending without losing their original keys |
| Related functions | sort() reindexes keys; arsort() sorts descending; ksort() sorts keys. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Confusing key sorting with value sorting | Check whether the comparison applies to keys or values before selecting the sort method. |
| Expecting an unchanged collection | The sorting operation updates the array or collection in place; copy it first when you need the original. |
| Ignoring comparison rules | The input is sorted in place. Values determine the ordering; keys stay attached to their values. |
Key Takeaways
asort()helps you sort values ascending without losing their original keys.- The input is sorted in place. Values determine the ordering; keys stay attached to their values.
- sort() reindexes keys; arsort() sorts descending; ksort() sorts keys.
- Use the practical example as a starting point, but validate real application inputs.
asort() is useful when you need to sort values ascending without losing their original keys. Use its documented rules and the examples above to choose the right behavior for your data.