Change language:

# array_product

(PHP 5 >= 5.1.0)

array_productCalculate the product of values in an array

### Opis

number array_product ( array `\$array` )

array_product() returns the product of values in an array.

`array`

The array.

### Zwracane wartości

Returns the product as an integer or float.

``` <?php\$a = array(2, 4, 6, 8);echo "product(a) = " . array_product(\$a) . "\n";echo "product(array()) = " . array_product(array()) . "\n";?> ```

```product(a) = 384
product(array()) = 1
```

### User Contributed Notes 5 notes

Marcel G
3 years ago
``` You can use array_product to calculate the factorial of n: <?php function factorial( \$n ) {   if( \$n < 1 ) \$n = 1;   return array_product( range( 1, \$n )); } ?> If you need the factorial without having array_product available, here is one: <?php function factorial( \$n ) {   if( \$n < 1 ) \$n = 1;   for( \$p++; \$n; ) \$p *= \$n--;   return \$p; } ?> ```
gmail at algofoogle
6 years ago
``` Just in relation to "bishop" and the overall behaviour of array_product... The "empty product" (i.e. product of no values) is supposed to be defined as "1": http://en.wikipedia.org/wiki/Empty_product ...however PHP's array_product() returns int(0) if it is given an empty array. bishop's code does this, too (so it IS a compatible replacement). Ideally, array_product() should probably return int(1). I guess it depends on your specific context or rationale. You might normally presume int(0) to be a suitable return value if there are no inputs, but let's say that you're calculating a price based on "percentage" offsets: \$price = 10.0; \$discounts = get_array_of_customer_discounts(); \$price = \$price * array_product(\$discounts); ...if there are NO "discounts", the price will come out as 0, instead of 10.0 ```
Andre D
7 years ago
``` This function can be used to test if all values in an array of booleans are TRUE. Consider: <?php function outbool(\$test) {     return (bool) \$test; } \$check[] = outbool(TRUE); \$check[] = outbool(1); \$check[] = outbool(FALSE); \$check[] = outbool(0); \$result = (bool) array_product(\$check); // \$result is set to FALSE because only two of the four values evaluated to TRUE ?> The above is equivalent to: <?php \$check1 = outbool(TRUE); \$check2 = outbool(1); \$check3 = outbool(FALSE); \$check4 = outbool(0); \$result = (\$check1 && \$check2 && \$check3 && \$check4); ?> This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop. ```
-1
``` An observation about the _use_ of array_product with primes: \$a=\$arrayOfSomePrimes=(2,3,11);               // 2 being the first prime (these days) \$codeNum=array_product(\$a); // gives 66 (== 2*3*11) echo "unique product(\\$a) = " . array_product(\$a) . "\n"; The 66 can (only) be split into its original primes, which can be transformed into their place in the row of primes (2,3,5,7,11,13,17,19...)  giving (1,2,3,4,5,6,7,8...) The 66 gives the places {1,2,5} in the row of primes. The number "66" is unique as a code for {1,2,5} So you can define the combination of table-columns {1,2,5} in "66". The bigger the combination, the more efficient in memory/transmission, the less in calculation. ```
``` gmail @ algofoogle is right, so we can extend our own array_product() to flexibly accept the empty product value.  Zero (0) is the default (to be compatible with PHP behavior), but you could change this to 1 for mathematical purposes or null for logical. <?php if (! function_exists('array_product')) {     function array_product(\$array, \$emptyProduct = 0) {         if (is_array(\$array)) {             return (0 == count(\$array) ? \$emptyProduct : array_reduce(\$array, '_array_product', 1));         } else {             trigger_error('Param #1 must be an array', E_USER_ERROR);             return false;         }     }     function _array_product(\$v,\$w) { return \$v * \$w; } } ?> ```