Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / PHP

usort()

Sort values by an integer comparator and replace every original key with a consecutive index.

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

Syntax

usort(array &$array, callable $callback): true

Sort values by an integer comparator and replace every original key with a consecutive index. In PHP 8 returning zero for two records preserves their order. Use a signed comparison, since a boolean comparator cannot distinguish before from equal. In PHP 8 returning bool from the comparator emits E_DEPRECATED.

Signature

usort(array &$array, callable $callback): true

Example output

[["A",1],["B",2],["C",2]]

Examples

<?php
$rows = ["x"=>["B",2],"y"=>["A",1],"z"=>["C",2]];
usort($rows, fn($a,$b) => $a[1] <=> $b[1]);
echo json_encode($rows);

Result

[["A",1],["B",2],["C",2]]

Sources