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.

Changelog

Version Description
5.3.6 The product of an empty array is now 1, when before this function would return 0 for an empty array.

Examples

Example #1 array_product() examples

<?php

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

?>

The above example will output:

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

Коментарии

Автор:
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.
2006-08-07 16:56:19
http://php5.kiev.ua/manual/ru/function.array-product.html
Автор:
You can use array_product to calculate the factorial of n:
<?php
function factorial$n )
{
  if( 
$n $n 1;
  return 
array_productrange1$n ));
}
?>

If you need the factorial without having array_product available, here is one:
<?php
function factorial$n )
{
  if( 
$n $n 1;
  for( 
$p++; $n; ) $p *= $n--;
  return 
$p;
}
?>
2010-09-28 11:36:54
http://php5.kiev.ua/manual/ru/function.array-product.html
Автор:
array_product() can be used to implement a simple boolean AND search

<?php
$args 
= array('first_name'=>'Bill','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Brenda','last_name'=>'Buzzard');
$values[] = array('first_name'=>'Victor','last_name'=>'Vulture');
$values[] = array('first_name'=>'Bill','last_name'=>'Blue Jay');
$values[] = array('first_name'=>'Bill','last_name'=>'Buzzard');

$result search_for($values,$args);
var_dump($result);exit;

function 
search_for($array,$args) {
   
$results = array();
    foreach (
$array as $row) {
       
$found false;
       
$hits = array();
        foreach (
$row as $k => $v) {
            if (
array_key_exists($k,$args)) $hits[$k] = ($args[$k] == $v);
        }

       
$found array_product($hits);
        if (!
in_array($row,$results) && true == $found$results[] = $row;
    }

    return 
$results;
}
?>

Output:

array (size=1)
  0 => 
    array (size=2)
      'first_name' => string 'Bill' (length=4)
      'last_name' => string 'Buzzard' (length=7)
2014-04-29 22:55:14
http://php5.kiev.ua/manual/ru/function.array-product.html
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
2017-06-05 09:04:55
http://php5.kiev.ua/manual/ru/function.array-product.html
Автор:
You can use array_product() to calculate the geometric mean of an array of numbers: 

<?php
$a 
= [ 110100 ];
$geom_avg powarray_product$a ), count$a ));
// = 9.999999999999998 ≈ 10
?>
2022-10-31 01:21:38
http://php5.kiev.ua/manual/ru/function.array-product.html
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);

?>
2023-03-20 17:38:32
http://php5.kiev.ua/manual/ru/function.array-product.html

    Поддержать сайт на родительском проекте КГБ