PHP | array_product()函数

这个 数组_乘积() 是PHP中的一个内置函数,它返回给定数组中所有数字的乘积。该函数只接受由数字组成的数组。如果数组中除数字外还有其他数据,则函数返回0。

null

语法:

array_product($array)

参数: 该函数有一个强制参数$array,我们希望计算所有值的乘积。

返回值: 此函数根据以下情况返回三个不同的值:

  • 如果数组至少包含一个非数字数据,则返回0。
  • 当一个空数组作为参数传递时,它返回1。
  • 如果以上两种情况都不满足,则返回数组中所有项的乘积。

例如:

Input : $array = [1, 2, 3, 4]
Output : 24 

Input : $array = [1, 'a'] 
Output : 0 

下面的程序演示了array_product()函数:

项目1: 用于演示array_product()函数的程序。

<?php
// PHP program to demonstrate
// the array_product() function
$a1 = array (1, 2, 3, 4);
echo ( array_product ( $a1 ));
?>


输出:

24

项目2: 当数组至少包含一个非数字数据时,用于演示array_product()函数的程序。

<?php
// PHP program to demonstrate the array_product()
// function when the array contains at least
// one non-number data
$a1 = array (1, 2, 3, 'a' );
echo ( array_product ( $a1 ));
?>


输出:

0

方案3: 当数组为空时,用于演示array_product()函数的程序。

<?php
// PHP program to demonstrate the array_product() function
// when the array is empty
$a1 = array ();
echo ( array_product ( $a1 ));
?>


输出:

1

参考 : http://php.net/manual/en/function.array-product.php

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享