array_product

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

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

说明

array_product ( array $array ) : number

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

参数

array

这个数组。

返回值

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

更新日志

版本 说明
5.3.6 空数组现在会产生 1,而之前此函数处理空数组会产生 0。

范例

Example #1 array_product() 例子

<?php

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

?>

以上例程会输出:

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

User Contributed Notes

bsr dot anwar at gmail dot com 05-Jun-2017 06:04
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
Jimmy PHP 29-Apr-2014 07:55
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)
Marcel G 28-Sep-2010 08:36
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;
}
?>
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.
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.
PHP8中文手册 站长在线 整理 版权归PHP文档组所有