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
array_product
(PHP 5 >= 5.1.0)
array_product — 配列の値の積を計算する
説明
number array_product ( array $array )array_product() は、配列の各要素の積を計算して integer または float で返します。
例 266. array_product() の例
<?php
$a = array(2, 4, 6, 8);
echo "product(a) = " . array_product($a) . "\n";
?>
上の例の出力は以下となります。
product(a) = 384
array_product
gmail at algofoogle
10-May-2007 01:18
10-May-2007 01:18
pqpqpq at wanadoo dot nl
17-Jan-2007 09:32
17-Jan-2007 09:32
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.
bishop
30-Nov-2006 12:13
30-Nov-2006 12:13
Yet another implementation of array_product() using PHP's native array_reduce():
if (! function_exists('array_product')) {
function array_product($array) {
if (is_array($array)) {
return (0 == count($array) ? 0 : 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; }
}
bishop
30-Nov-2006 12:04
30-Nov-2006 12:04
Regarding Andre D function to test if all values in an array of booleans are true, you can also use:
<?php
$allTrue = (! in_array(false, $arrayToCheck));
?>
Both this method and Andre D's are O(n), but this method has a lower k in the average case: in_array() stops once it finds the first false, while array_product must always traverse the entire array.
marcel at computingnews dot com
16-Nov-2006 02:07
16-Nov-2006 02:07
if you don't have PHP 5.xx . you can use this function.
It does not make sure that the variables are numeric.
function calculate_array_product($array="")
{
if(is_array($array))
{
foreach($array as $key => $value)
{
$productkey = $productkey + $key;
}
return $productkey;
}
return NULL;
}
Andre D
08-Aug-2006 05:56
08-Aug-2006 05: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 11:25
06-Jan-2006 11: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