php array product,PHP array_product()用法及代码示例
在某些時候需要計算數組中所有元素的乘積。執行此操作的最基本方法是遍歷所有元素并計算乘積,但是PHP為我們提供了內置函數來執行此操作。 array_product()是PHP中的內置函數,用于查找數組中所有元素的乘積。
用法:
array_product($array)
參數:該函數僅使用一個參數$array,該參數指向我們希望獲取其元素乘積的輸入數組。
返回值:array_product()函數根據數組元素的性質返回整數或浮點值。
例子:
Input : array = (5, 8, 9, 2, 1, 3, 6)
Output : 12960
Input : array = (3.2, 4.8, 9.1, 4.36, 1.14)
Output : 694.7426304
以下示例程序旨在說明array_product()函數的工作方式:
當傳遞給array_product()函數的數組僅包含整數值時,array_product()函數將返回一個整數值,該整數值等于傳遞給它的數組所有元素的乘積。
// PHP function to illustrate the use
// of array_product()
// Return Integer number
function Product($array)
{
$result = array_product($array);
return($result);
}
$array = array(5, 8, 9, 2, 1, 3, 6);
print_r(Product($array));
?>
輸出:
12960
當傳遞給array_product()函數的數組包含整數和浮點值時,則array_product()函數將返回一個浮點值,該值等于傳遞給它的數組所有元素的乘積。
// PHP function to illustrate the use of
// array_product()
function Product($array)
{
$result = array_product($array);
return($result);
}
$array = array(3.2, 4.8, 9.1, 4.36, 1.14);
print_r(Product($array));
?>
輸出:
694.7426304
總結
以上是生活随笔為你收集整理的php array product,PHP array_product()用法及代码示例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java语言类的作用,java 语言Cl
- 下一篇: matlab解常微分方程,Matlab中