Home > Backend Development > PHP Problem > How to calculate the product and sum of php arrays

How to calculate the product and sum of php arrays

青灯夜游
Release: 2023-03-16 06:26:01
Original
2721 people have browsed it

How to calculate the product and sum of PHP arrays: 1. Use array_product() to calculate the product of all elements in the array, the syntax is "array_product($arr)"; 2. Use array_sum() to calculate The sum of all elements of an array, syntax "array_sum($arr)".

How to calculate the product and sum of php arrays

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

php array calculation product and summation methods

PHP has two built-in functions that can calculate array products and sums. Let’s take a look.

1. Use the array_product() function to calculate the array product

array_product() function is used to calculate the product of all elements in the array

array_product ($arr)
Copy after login
  • If all elements of array arr are integers, return an integer value; if one or more of the values ​​are floating point numbers, return a floating point number.

  • If there are non-numeric elements in the array arr, PHP will try to convert them to a numeric value, and if the conversion fails, it will be treated as a 0 value. For example, the string "29.5" will be converted to the decimal 29.5, and the string "100abc" will be converted to the integer 100.

Example:

<?php
$a = array(2, 3, 10, 15);
echo array_product($a) . "<br />";
$b = array(2, 5.5, "10abc", "3.3");
echo array_product($b) . "<br />";
$c = array(4, 3.5, "0.1", "hello");
echo array_product($c);
?>
Copy after login

How to calculate the product and sum of php arrays

2. Use array_sum() to implement array summation

array_sum() function is used to calculate the sum of all elements in an array. Its syntax is as follows:

array_sum ( $arr )
Copy after login
  • If all elements of array arr are integers, return an integer value; if If one or more of the values ​​is a floating point number, a floating point number is returned.

  • If there are non-numeric elements in the array arr, PHP will try to convert them to a numeric value, and if the conversion fails, it will be treated as a 0 value. For example, the string "45" will be converted to the integer 45, and the string "12.4abc" will be converted to the decimal 12.4.

Example:

<?php
$a = array(4, 2, 3, 10);
echo array_sum($a) . "<br />";
$b = array("10.1xy", 100, &#39;1&#39;, "0.01");
echo array_sum($b);
?>
Copy after login

How to calculate the product and sum of php arrays

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to calculate the product and sum of php arrays. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template