百科狗-知识改变命运!

array_product() - 计算数组中所有值的乘积 - php 数组函数

百变鹏仔2年前 (2023-11-21)阅读数 27#技术干货
文章标签乘积

array_product()

(PHP 5 >= 5.1.0, PHP 7)

计算数组中所有值的乘积

说明

array_product(array $array):number

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

array_product() - 计算数组中所有值的乘积 - php 数组函数

参数

$array

这个数组。

返回值

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

更新日志

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

范例

Example #1array_product()例子


以上例程会输出:

product(a) = 384
product(array()) = 1
This function can be used to test if all values in an array of booleans are TRUE.
Consider:

The above is equivalent to:

This use of array_product is especially useful when testing an indefinite number of booleans and is easy to construct in a loop.
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
You can use array_product to calculate the factorial of n:

If you need the factorial without having array_product available, here is one:

array_product() can be used to implement a simple boolean AND search

Output:
array (size=1)
 0 => 
  array (size=2)
   'first_name' => string 'Bill' (length=4)
   'last_name' => string 'Buzzard' (length=7)
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.

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)
{if $zbp->Config('yd1125')->foot}