Arrays7 min readExamples run on PHP 8.5.10

array_product()

Learn how PHP 8.4 array_product() multiplies array values, what it returns for empty arrays and mixed types, and which mistakes to avoid.

Signature

array_product(array $array): int|float
Available since PHP 5.1.07.48.08.18.28.38.48.5All versions →

Purpose

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.

Example
<?php
$numbers = [2, 4, 6, 8];
echo array_product($numbers), "\n";
echo array_product([]), "\n";
Output
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.

Example
<?php
var_dump(array_product([2, 3, 4]));
var_dump(array_product([1.5, 4]));
var_dump(array_product(["3", "4"]));
Output
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.

Example
<?php
$options = ['sizes' => 3, 'colors' => 4, 'materials' => 2];
echo json_encode(['combinations' => array_product($options)]), "\n";
Output
{"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 0 makes the whole product 0. Because zero wins regardless of position, a 0 in 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.1 and 0.2 may carry tiny binary rounding errors. Round for display or compare with a tolerance.

Common mistakes

  1. Expecting 0 for an empty array. That is the result of array_sum(), not array_product().
  2. 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.
  3. 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.
  4. 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.
  5. Forgetting that keys do not matter. If you want to multiply only some entries, filter the array first. array_product() always uses every value.
  • 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

Basic product and empty array
<?php
$numbers = [2, 4, 6, 8];
echo array_product($numbers), "\n";
echo array_product([]), "\n";
Output
384
1
Result types for ints, floats and numeric strings
<?php
var_dump(array_product([2, 3, 4]));
var_dump(array_product([1.5, 4]));
var_dump(array_product(["3", "4"]));
Output
int(24)
float(6)
int(12)
Counting combinations from an associative array
<?php
$options = ['sizes' => 3, 'colors' => 4, 'materials' => 2];
echo json_encode(['combinations' => array_product($options)]), "\n";
Output
{"combinations":24}