Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

asort()

Sort values in ascending order while retaining the original keys.

Version: PHP 8.4; repository runtime, production runtime not attested

Syntax

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

Sort values in ascending order while retaining the original keys. SORT_STRING compares the textual forms, so "10" precedes "2", whereas SORT_NUMERIC compares magnitudes. The call mutates its argument and returns true. In PHP 8 equal values keep their prior order. The internal pointer is reset to the first sorted entry.

Signature

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

Example output

[["ten","two"],["two","ten"]]
[["p","q","z"],"p"]

Examples

<?php
$prices = ["ten"=>"10","two"=>"2"];
$lexical = $prices; asort($lexical,SORT_STRING);
asort($prices,SORT_NUMERIC);
echo json_encode([array_keys($lexical),array_keys($prices)]);
$ties=["z"=>3,"p"=>1,"q"=>1]; end($ties); asort($ties);
echo PHP_EOL,json_encode([array_keys($ties),key($ties)]);

Result

[["ten","two"],["two","ten"]]
[["p","q","z"],"p"]

Sources