Skip to content

Core\Arr::max

Core\Arr::max(array<T> $a): ?TCore\Arr::max(
    array<T> $a,
): ?T

Gives you the largest value in an array.

The values are compared the way Core\Arr::sort orders them, so the largest value and the last value of the sorted array are always the same entry. Numbers compare as numbers. Texts compare byte by byte, so "9" is larger than "10" and a text is never read as a number.

Two values of different kinds have no order between them. An array that holds the number 0 and the text "a" throws an error instead of giving an answer.

When several entries are equally large you get the first of them. The keys are not part of the question: only the values are compared, and only a value is returned. Use Core\Arr::keyOf when you also need the key of the entry you found. An array with no entries gives you null.

The examples show the largest number in a list, the best score together with the name that holds it, and the width of a column in a printed table.

Parameters

  • $aarray<T>The array to search.

Return value

Returns ?T. The largest value, the first of equals; null for the empty array. Two strings compare bytewise, so max(["1e2", "50"]) is "50".

Errors

  • RuntimeErrorTwo entries have no natural order, such as an int and a string, or any object.

Examples

The largest number

<?nvs
// Shows how to read the largest value of an array.
array<int> $temperatures = [7, -2, 13, 4];
// This prints 13.
echo Core\Arr::max($temperatures) ?? 0, "\n";
// A map works the same way. Only the values are compared, so this prints 91.
array<int> $scores = ['ada' => 42, 'lin' => 91, 'alan' => 67];
echo Core\Arr::max($scores) ?? 0, "\n";
// Texts compare byte by byte. This prints plum.
array<string> $fruit = ['pear', 'apple', 'plum'];
echo Core\Arr::max($fruit) ?? '', "\n";
Output
13
91
plum

The best score and who got it

<?nvs
// Shows how to read the highest score and the name that holds it.
array<int> $scores = ['ada' => 42, 'lin' => 91, 'alan' => 67];
// `Core\Arr::max` gives you the value. The `?? 0` covers the case of a list
// with no scores in it. This prints 91.
int $best = Core\Arr::max($scores) ?? 0;
echo 'best score: ', $best, "\n";
// `Core\Arr::keyOf` gives you the key of the first entry with that value.
// This prints lin.
echo 'who: ', Core\Arr::keyOf($scores, $best) ?? 'nobody', "\n";
Output
best score: 91
who: lin

The width of a column

<?nvs
// Shows how to find the width of a column before printing a table. Every name
// is padded to the width of the longest one.
array<string> $names = ['Ada', 'Grace Hopper', 'Lin'];
// `Core\Str::length` counts the characters of one name, and `Core\Arr::map`
// does that for every name in the list.
array<uint> $widths = Core\Arr::map($names, fn(string $name): uint => Core\Str::length($name));
// The widest name has 12 characters, so the column is 12 wide.
uint $column = Core\Arr::max($widths) ?? (0 as uint);
// The second column now starts in the same place on every line.
foreach ($names as string $name) {
echo Core\Str::padEnd($name, $column, ' '), ' | ready', "\n";
}
Output
Ada | ready
Grace Hopper | ready
Lin | ready