How to sum multiple arrays in php

青灯夜游
Release: 2023-03-17 18:20:01
Original
1219 people have browsed it

Implementation steps: 1. Use the array_sum() function to find the sum of the elements in multiple arrays. The syntax is "$s1=array_sum($arr1);$s2=array_sum($arr2);... $sN=array_sum($arrN);"; 2. Use the " " operator to add the sum of the obtained elements to find the sum of multiple arrays, the syntax is "$sum = $s1 $s2 .... $sN;" .

How to sum multiple arrays in php

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

In php, you can use array_sum() function and " " operator to implement the sum of multiple arrays.

Implementation steps:

Step 1: Use the array_sum() function to find the sum of the elements in multiple arrays

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

$sum1=array_sum($arr1);
$sum2=array_sum($arr2);
$sum3=array_sum($arr3);
$sum4=array_sum($arr4);
...
$sumN=array_sum($arrN);
Copy after login

Step 2: Use the " " operator to add the sum of the obtained elements to find the sum of multiple arrays

$sum = $sum1 + $sum2 + $sum3 + $sum4 +....+ $sumN;
Copy after login

Implementation example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr1 = array(1,2,3,4,5);
$arr2 = array(6,7,8,9,10);
$arr3 = array(1,3,5,7,9);
$arr4 = array(2,4,6,9,10);

$sum1=array_sum($arr1);
echo &#39;$arr1的元素总和:&#39;.$sum1;
$sum2=array_sum($arr2);
echo &#39;<br>$arr2的元素总和:&#39;.$sum2;
$sum3=array_sum($arr3);
echo &#39;<br>$arr3的元素总和:&#39;.$sum3;
$sum4=array_sum($arr4);
echo &#39;<br>$arr4的元素总和:&#39;.$sum4;

$sum = $sum1 + $sum2 + $sum3 + $sum4;

echo &#39;<br>多个数组的总和:&#39;.$sum;
?>
Copy after login

How to sum multiple arrays in php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to sum multiple arrays in php. 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