array_product

Multiplies all values in an array together and returns the product as an int or float.

Signature
array_product(array $array): int|float
  • Added in PHP 5.1.0

Parameters

ParameterTypeDescription
$arrayarrayThe array whose values are multiplied together.

Return value

int|floatThe product of all values. Returns the integer 1 for an empty array, and a float if any value is a float or the result overflows the integer range.

Errors

Emits an E_WARNING (since PHP 8.3) for values that cannot be converted to a number; those values are skipped rather than aborting the calculation.

Changelog

  • PHP 8.3.0Emits E_WARNING when a value cannot be converted to int or float. Previously arrays and objects were skipped and every other value was cast to int. Objects defining a numeric cast, such as GMP, are now used instead of being ignored.

array_product() multiplies every value in an array and returns the result. It is the multiplicative twin of array_sum() and, like it, is written in C — considerably faster than an equivalent array_reduce() with a closure.

The empty array returns 1

An empty array yields 1, not 0. That is the correct mathematical identity for multiplication, but it means an empty result set silently produces a plausible-looking total. If "no items" should mean something different in your domain, test $arr === [] before calling.

Types and overflow

The return type is int when every value is an integer and the product fits; otherwise you get a float. Integer overflow is not an error — PHP quietly promotes to float, and past roughly 2^53 the result stops being exact. Multiplying many large integers is where this bites.

When to reach for it

Reach for it when combining independent multipliers: the volume of a box from its dimensions, the number of variant combinations in a product matrix, a compound discount factor, or the joint probability of independent events. Since PHP 8.3, non-numeric values raise an E_WARNING instead of being silently treated as 0 or skipped, so run the array through array_filter('is_numeric', ...) first if the input is untrusted.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Compute the volume of a box from width, height and depth
  • Count how many variant combinations a product with several option lists has
  • Multiply a chain of discount or tax factors into one multiplier
  • Combine the probabilities of independent events into a joint probability

Frequently asked questions

Why does array_product return 1 for an empty array?

One is the multiplicative identity, so it is the mathematically consistent result — multiplying nothing leaves any running total unchanged. Check for an empty array yourself if your domain needs a different answer.

Does array_product return an int or a float?

It returns an int when every value is an integer and the product fits in PHP_INT_MAX; otherwise it returns a float. Overflow is silent, so large products lose precision without any error.

What happens if the array contains strings?

Numeric strings are converted and multiplied normally. Since PHP 8.3, values that cannot be converted to a number emit an E_WARNING and are skipped rather than being treated as zero.

More examples

Counting variant combinations

array_map('count', ...) turns each option list into its size, giving [3, 2, 2], and the product is 12 — the number of distinct combinations. The empty-array identity of 1 is also correct here: a product with no option lists has exactly one variant.

Example · php
<?php
$options = [
    'size'  => ['S', 'M', 'L'],
    'color' => ['red', 'blue'],
    'fit'   => ['slim', 'regular'],
];
$counts = array_map('count', $options);
print_r($counts);
echo array_product($counts), " variants\n";

Integer overflow becomes a float

The first product fits in an int and returns int(1000000000). The second exceeds PHP_INT_MAX, so PHP promotes to float and returns an approximate value — no error, no warning, just a loss of precision you have to anticipate.

Example · php
<?php
$small = [1000, 1000, 1000];
var_dump(array_product($small));

$big = [PHP_INT_MAX, 2];
var_dump(array_product($big));

Guarding non-numeric input

Filtering with is_numeric removes 'n/a' and the empty string, leaving ['2', 5] and a product of 10. Without the filter, PHP 8.3 and later would emit an E_WARNING for each unconvertible value.

Example · php
<?php
$raw = ['2', 'n/a', 5, ''];
$clean = array_filter($raw, 'is_numeric');
print_r($clean);
echo array_product($clean), "\n";
echo "skipped ", count($raw) - count($clean), " values\n";

Discussion

  • Be the first to comment on this lesson.