Skip to content

Kirby 6.0.0

A::sort()

Sorts a multi-dimensional array by a certain column

A::sort(array $array, string $field, string $direction = 'desc', int $method = 0): array

Parameters

Name Type Default Description
$arrayrequired array<array-key, mixed> no default value
$fieldrequired string no default value
$direction string 'desc' desc (descending) or asc (ascending)
$method int 0 A PHP sort method flag

Return type

array<array-key, mixed>

Parent class

Kirby\Toolkit\A

Examples

$array = [
  ['id' => 1, 'username' => 'mike'],
  ['id' => 2, 'username' => 'peter'],
  ['id' => 3, 'username' => 'john']
];

$sorted = A::sort($array, 'username', 'asc');
// result: [
//   2 => ['id' => 3, 'username' => 'john'],
//   0 => ['id' => 1, 'username' => 'mike'],
//   1 => ['id' => 2, 'username' => 'peter']
// ]