search
HomeBackend DevelopmentPHP ProblemHow to find the sum of array elements in php

Method: 1. Use array_sum() to calculate the sum of all elements in the array. The syntax is "array_sum($arr)"; 2. Use the for statement to loop through the array and add the elements one by one. , the syntax is "for($i=0;$i

How to find the sum of array elements in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

PHP provides multiple methods to add array elements Sum, three types are introduced below.

Method 1: Use array_sum() function

array_sum() is a built-in function in PHP that can calculate the value of all elements in an array and , and returns the sum of the elements.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array(1,2,3,4,5,6,7,8,9,10);
var_dump($array);
echo &#39;数组所有元素之和:&#39;. array_sum($array);
?>

How to find the sum of array elements in php

Description:

  • If all elements in $array are integers, return an integer value; if there is one or more values ​​are floating point numbers, a floating point number is returned.

  • If there are non-numeric elements in $array, PHP will convert them into a numeric value (PHP is a weak language type and will automatically convert the variable into Correct data type), if the conversion fails, it will be used as a value of 0 to participate in the calculation.

<?php
header("Content-type:text/html;charset=utf-8");
$array= array("10.1xy", 100, &#39;1&#39;, "0.01");
var_dump($array);
echo &#39;数组所有元素之和:&#39;. array_sum($array);
?>

How to find the sum of array elements in php

Method 2: Use the for statement to loop through the array and add the elements one by one

<?php
$array= array(1,2,3,4,5,6,7,8,9,10);
$sum=0;
$len=count($array);//数组长度
for ($i=0; $i < $len; $i++) { 
    $sum+=$array[$i];
} 
echo &#39;1 + 2 + 3 +...+ 9 + 10 = &#39;. $sum;
?>

How to find the sum of array elements in php

Note: The for statement is only valid for index arrays and cannot traverse associated arrays

The for loop will control the loop The variable of times is predefined in the for statement, so the for loop statement can perform loop operations according to the known number of loops, which is suitable for situations where the number of times the script needs to be run is clearly known (based on the numerical subscript of the index array).

Method 3: Use foreach to loop through the array and add the elements one by one.

foreach is specially designed for traversing arrays Statement is a commonly used method when traversing arrays, which provides great convenience in traversing arrays; after PHP5, objects can also be traversed (foreach can only be applied to arrays and objects).

The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.

<?php
header(&#39;content-type:text/html;charset=utf-8&#39;);   
$array= array(1,2,3,4,5,6,7,8,9,10);
$sum=0;
foreach ($array as $value) { 
    $sum+=$value;
} 
echo &#39;数组所有元素之和:&#39;. $sum;
?>

How to find the sum of array elements in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to find the sum of array elements in php. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.