Skip to content

Core\Arr::product

Multiplies all the values of an array together and gives you the result.

The values are multiplied one at a time, and the type of the result follows what it meets. Whole numbers multiplied by whole numbers stay whole. One float anywhere makes the result a float. One decimal anywhere makes the result a decimal, which is the type to use for money.

A result that grows past the range a whole number covers throws an error. It does not wrap around and it does not turn into a float. Multiplication grows fast, so a few large numbers are enough to reach that point.

A float and a decimal in the same array throw an error as well. There is no type that holds both of them, so there is no result to give you.

An array with no entries gives you 1. A single value of 0 anywhere makes the result 0.

The examples show a list of whole numbers, three discounts applied one after the other, and the number of variants a shop article has.

Parameters

  • $aarray<int|float|decimal>The numbers to multiply.

Return value

Returns int|float|decimal. The product, of the widest type met; 1 as an int for the empty array.

Errors

  • RuntimeErrorThe running int or decimal product goes outside its type's range. It is never converted to float. The error is also thrown when one array has both a float and a decimal, because they have no common type.

Examples

Multiplying a list of numbers

<?nvs
// Shows how to multiply the values of an array together.
array<int> $factors = [2, 3, 7];
// This prints 42. The keys are not used, only the values.
echo Core\Arr::product($factors), "\n";
// A map works the same way. This prints 120.
array<int> $crates = ['small' => 4, 'medium' => 5, 'large' => 6];
echo Core\Arr::product($crates), "\n";
// One value of 0 makes the result 0. This prints 0.
array<int> $withZero = [8, 0, 9];
echo Core\Arr::product($withZero), "\n";
// An array with no entries gives you 1. This prints 1.
array<int> $nothing = [];
echo Core\Arr::product($nothing), "\n";
Output
42
120
0
1

Three discounts one after the other

<?nvs
// Shows how to apply several discounts to a price. Each discount is a factor,
// and all the factors multiplied together give the one factor to use.
// 10 percent off, then 5 percent, then 2 percent.
array<decimal> $discounts = [0.90, 0.95, 0.98];
// One `decimal` in the array makes the result a `decimal`, so no fraction is
// lost. This prints 0.837900.
echo Core\Arr::product($discounts), "\n";
decimal $price = 240.00;
// The price with all three discounts taken off it. This prints 201.09600000.
echo $price * Core\Arr::product($discounts), "\n";
Output
0.837900
201.09600000

How many variants an article has

<?nvs
// Shows how to count the variants of an article in a shop. Every option has
// its own choices, and all the counts multiplied together give the number of
// variants a customer can order.
array<array<string>> $options = [
'size' => ['S', 'M', 'L', 'XL'],
'colour' => ['black', 'white'],
'print' => ['none', 'logo', 'photo'],
];
// `Core\Arr::map` turns every option into how many choices it has.
array<int> $counts = Core\Arr::map(
$options,
fn(array<string> $choices): int => Core\Arr::count($choices) as int,
);
// This prints 24.
echo 'variants: ', Core\Arr::product($counts), "\n";
Output
variants: 24