Hướng dẫn
Quảng cáo

Hàm array_product() trong PHP

Hướng dẫn cách sử dụng hàm array_product() về mảng trong lập trình PHP

Tác dụng của hàm array_product()

The array_product() function calculate the product of values in an array.

The following table summarizes the technical details of this function.

Return Value: Returns the product as an integer or float.
Changelog: Since PHP 5.3.6, the product of an empty array is 1. In earlier versions, this function would return 0 for an empty array.
Version: PHP 5.1.0+


The basic syntax of the array_product() function is given with:


The following example shows the array_product() function in action.

// Sample array
$numbers = array(2, 5, 8, 10, 15);
// Getting the product of array values
echo array_product($numbers); // Prints: 12000


The array_product() function accepts the following parameters.

Parameter Description
array Required. Specifies the array to work on.

More Examples

Here're some more examples showing how array_product() function actually works:

If any value in the array cannot be evaluated to a number, this function returns the integer 0.

// Sample array
$mixed = array(2, 5, "hello", 10, 15);
// Getting the product of array values
echo array_product($mixed); // Prints: 0

Bài viết này đã giúp ích cho bạn?

Bài viết mới