PHP 8.6.0 Beta 1 is available for testing

array_product

(PHP 5 >= 5.1.0, PHP 7, PHP 8)

array_productCalcula el producto de los valores del array

Descripción

function array_product(array $array): int |float

array_product() devuelve el producto de los valores del array array.

Parámetros

array

El array.

Valores devueltos

Devuelve el producto, en forma de int o de float .

Historial de cambios

Versión Descripción
8.3.0 Emite ahora un E_WARNING cuando los valores de tipo array no pueden ser convertidos en int o float . Anteriormente, los arrays y los objetos eran ignorados mientras que todos los demás valores eran convertidos en int . Además, los objetos que definen una conversión numérica (por ejemplo, GMP ) son ahora convertidos en lugar de ser ignorados.

Ejemplos

Ejemplo #1 Ejemplo con array_product()

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

El ejemplo anterior mostrará:

producto(a) = 384
producto(array()) = 1

Found A Problem?

Learn How To Improve This PageSubmit a Pull RequestReport a Bug
+add a note

User Contributed Notes 5 notes

up
28
Andre D
20 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.
up
15
bsr dot anwar at gmail dot com
9 years ago
Here's how you can find a factorial of a any given number with help of range and array_product functions.
function factorial($num) {
 return array_product(range(1, $num));
}
printf("%d", factorial(5)); //120
up
3
gergely dot lukacsy at streamnet dot hu
3 years ago
Just a little correction for Andre D's answer: "(bool) array_product($array);" is equivalent with the conjunction of each array elements of $array, UNLESS the provided array is empty in which case array_product() will return 1, which will translate to boolean TRUE.
To mitigate this, you should expand the function with an additional check:
<?php
$result = !empty($check) && !!array_product($check);
?>
up
1
biziclop
3 years ago
You can use array_product() to calculate the geometric mean of an array of numbers: 
<?php
$a = [ 1, 10, 100 ];
$geom_avg = pow( array_product( $a ), 1 / count( $a ));
// = 9.999999999999998 ≈ 10
?>
up
0
Marcel G
15 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;
}
?>
+add a note

AltStyle によって変換されたページ (->オリジナル) /