array_product

(PHP 5 >= 5.1.0RC1)

array_product -- 计算数组中所有值的乘积

说明

number array_product ( array array )

array_product() 以整数或浮点数返回一个数组中所有值的乘积。

例子 1. array_product() 例子

<?php

$a
= array(2, 4, 6, 8);
echo
"product(a) = " . array_product($a) . "\n";

?>

上例将输出:

product(a) = 384


add a note add a note User Contributed Notes
Andre D
08-Aug-2006 04:56
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.
mattyfroese at gmail dot com
06-Jan-2006 10:25
If you don't have PHP 5

$ar = array(1,2,3,4);
$t = 1;
foreach($ar as $n){
   $t *= $n;
}
echo $t; //output: 24