Home > Article > Backend Development > How to sum multiple arrays in php
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;" .

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);
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;
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 '$arr1的元素总和:'.$sum1;
$sum2=array_sum($arr2);
echo '<br>$arr2的元素总和:'.$sum2;
$sum3=array_sum($arr3);
echo '<br>$arr3的元素总和:'.$sum3;
$sum4=array_sum($arr4);
echo '<br>$arr4的元素总和:'.$sum4;
$sum = $sum1 + $sum2 + $sum3 + $sum4;
echo '<br>多个数组的总和:'.$sum;
?>
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!