Signature
array_product(array $array): int|floatPurpose
array_product() multiplies every value in an array and returns the result. It is the multiplicative counterpart of array_sum(). Typical uses are counting combinations of options, scaling by a chain of factors, or computing a volume from a list of dimensions.
Signature
Parameters and defaults
The function takes a single required parameter, $array. It has no defaults and no flags.
- Only the values take part in the multiplication. Keys are ignored, so a list and an associative array holding the same values give the same product.
- Values are converted to numbers as needed. Integers and floats are used as they are, and numeric strings such as
"3"are treated as numbers.
Return value and failure cases
The function returns an int when the multiplication stays within integer range and every operand is an integer. It returns a float when any operand is a float, or when an integer product overflows the platform integer range.
An empty array returns 1, the multiplicative identity. This is easy to forget: the product of nothing is 1, not 0.
The function does not throw for an array containing odd values. As of PHP 8.3.0, it emits an E_WARNING when an array value cannot be converted to an int or float. Before 8.3, arrays and objects were ignored and other values were cast to int. Objects with a numeric cast, such as GMP, are now cast instead of ignored. Passing something that is not an array is a type error, because the parameter is declared as array.
Example 1: the basics
This example multiplies four integers and then an empty array.
$numbers = [2, 4, 6, 8];
echo array_product($numbers), "\n";
echo array_product([]), "\n";384 1
The first line is 2 * 4 * 6 * 8. The second shows the identity value 1.
Example 2: result types
The result type follows normal PHP arithmetic. Use var_dump() to see it.
var_dump(array_product([2, 3, 4]));
var_dump(array_product([1.5, 4]));
var_dump(array_product(["3", "4"]));int(24) float(6) int(12)
Integers give an int. A float operand makes the whole product a float, even when the value is whole, as in 6. Numeric strings are converted to numbers, here giving an int.
Example 3: counting combinations
If a product can be configured with 3 sizes, 4 colors and 2 materials, the number of distinct variants is the product of the option counts. Keys are ignored, so descriptive names are fine.
$options = ['sizes' => 3, 'colors' => 4, 'materials' => 2];
echo json_encode(['combinations' => array_product($options)]), "\n";{"combinations":24}
Edge cases
- Empty array: returns
1. If an empty input should mean "no result", check$array === []before calling the function. - Zero: a single
0makes the whole product0. Because zero wins regardless of position, a0in the data can hide the other values. - Overflow: when integer multiplication exceeds the integer range, PHP continues with floating-point arithmetic. The result is then an approximation, so do not rely on exact integer digits for very large products.
- Negative numbers: the sign follows normal rules. An odd number of negative values gives a negative product.
- Floating-point rounding: products of floats such as
0.1and0.2may carry tiny binary rounding errors. Round for display or compare with a tolerance.
Common mistakes
- Expecting
0for an empty array. That is the result ofarray_sum(), notarray_product(). - Passing unvalidated data. Strings like
"abc"cannot be converted to a number. Since PHP 8.3 this raises a warning instead of being quietly ignored. Validate or filter input first. - Assuming nested arrays are flattened. The function does not recurse. An array inside the array is not a number, so flatten or reduce it yourself first.
- Relying on exact large integers. After overflow the value is a float. If you need arbitrary precision, use an extension designed for it, such as GMP or BCMath, when available.
- Forgetting that keys do not matter. If you want to multiply only some entries, filter the array first.
array_product()always uses every value.
Related functions
array_sum()adds the values in an array instead of multiplying them.array_reduce()folds an array with your own callback, which is useful for custom rules such as skipping zeros.array_filter()removes unwanted values before you multiply.array_map()transforms values, for example converting percentages into multipliers.
Recap
array_product() multiplies all values in an array and ignores keys. It returns an int or float, gives 1 for an empty array, and since PHP 8.3 warns about values that cannot be converted to numbers. Remember the zero and overflow cases, and validate data before multiplying.
Tested examples
$numbers = [2, 4, 6, 8];
echo array_product($numbers), "\n";
echo array_product([]), "\n";
384 1
var_dump(array_product([2, 3, 4]));
var_dump(array_product([1.5, 4]));
var_dump(array_product(["3", "4"]));
int(24) float(6) int(12)
$options = ['sizes' => 3, 'colors' => 4, 'materials' => 2];
echo json_encode(['combinations' => array_product($options)]), "\n";
{"combinations":24}