Skip to content

Core\Arr::min

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

Gives you the smallest value in an array.

The values are compared the way Core\Arr::sort orders them, so the smallest value and the first value of the sorted array are always the same entry. Numbers compare as numbers. Texts compare byte by byte, so "1e2" is smaller than "50" 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. An array that holds only numbers, only texts, only true and false, or only objects of one class that can be compared always has an answer.

The keys are not part of the question. Only the values are compared, and only a value is returned. An array with no entries gives you null.

The examples show the smallest number in a list, the empty array, and the earliest delivery date in a set of orders.

Parameters

  • $aarray<T>The array to search.

Return value

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

Errors

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

Examples

The smallest number

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

An array with no entries

<?nvs
// Shows what an array with no entries gives you, and two ways to handle it.
array<int> $prices = [];
// There is no smallest value here, so the result is `null`. This prints
// no prices yet.
?int $lowest = Core\Arr::min($prices);
if ($lowest == null) {
echo "no prices yet\n";
} else {
echo 'lowest: ', $lowest, "\n";
}
// `??` gives you a value to use when the result is `null`. This prints 0.
echo Core\Arr::min($prices) ?? 0, "\n";
// One entry is enough for an answer. This prints 5.
array<int> $one = [5];
echo Core\Arr::min($one) ?? 0, "\n";
Output
no prices yet
0
5

The earliest delivery date

<?nvs
// Shows how to find the earliest delivery date in a list of orders. A date
// written as `YYYY-MM-DD` compares byte by byte in the same order as time, so
// no date library is needed for this question.
array<array<string>> $orders = [
['id' => 'A-17', 'delivery' => '2026-03-04'],
['id' => 'A-18', 'delivery' => '2026-01-29'],
['id' => 'A-19', 'delivery' => '2026-11-02'],
];
// `Core\Arr::column` takes one field out of every order.
array<string> $dates = Core\Arr::column($orders, 'delivery');
// This prints 2026-01-29.
echo 'first delivery: ', Core\Arr::min($dates) ?? 'none', "\n";
Output
first delivery: 2026-01-29