array_product() Function
array_product()是PHP中的一个内建函数,它返回给定数组中所有数字的乘积。该函数接受一个仅由数字组成的数组。如果数组中除了数字以外还有其他数据,则函数返回0。
语法
1 | array_product($array) |
参数:该函数有一个必须的参数数组,我们想计算所有值的乘积。
返回值:该函数根据以下情况返回三个不同的值:
- 如果数组至少包含一个非数字数据,则返回0
- 当空数组作为参数传递时,它返回1。
- 如果以上两种情况都不满足,则返回数组中所有项的乘积。
例子
1 2 3 4 5 | Input : $array = [1, 2, 3, 4] Output : 24 Input : $array = [1, 'a'] Output : 0 |
下面的程序演示了array_product()函数:
程序1:演示array_product()函数的程序。
1 2 3 4 5 6 7 | <?php // PHP program to demonstrate // the array_product() fucntion $a1=array(1, 2, 3, 4); echo(array_product($a1)); ?> |
输出
1 | 24 |
程序2:当数组至少包含一个非数字数据时,演示array_product()函数的程序。
1 2 3 4 5 6 7 8 9 | <?php // PHP program to demonstrate the array_product() // fucntion when the array contains at least // one non-number data $a1=array(1, 2, 3, 'a'); echo(array_product($a1)); ?> |
输出
1 |
程序3:在数组为空时演示array_product()函数的程序。
1 2 3 4 5 6 7 8 | <?php // PHP program to demonstrate the array_product() fucntion // when the array is empty $a1=array(); echo(array_product($a1)); ?> |
输出
1 | 1 |
参考