PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

array_push> <array_pop
Last updated: Fri, 05 Sep 2008

view this page in

array_product

(PHP 5 >= 5.1.0)

array_productCalculate the product of values in an array

Description

number array_product ( array $array )

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

Parameters

array

The array.

Return Values

Returns the product as an integer or float.

Examples

Example #1 array_product() examples

<?php

$a 
= array(2468);
echo 
"product(a) = " array_product($a) . "\n";

?>

The above example will output:

product(a) = 384



array_push> <array_pop
Last updated: Fri, 05 Sep 2008
 
add a note add a note User Contributed Notes
array_product
gmail at algofoogle
09-May-2007 09:18
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
pqpqpq at wanadoo dot nl
17-Jan-2007 04: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
29-Nov-2006 07: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
29-Nov-2006 07: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
15-Nov-2006 09: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
07-Aug-2006 01: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 06: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

array_push> <array_pop
Last updated: Fri, 05 Sep 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites