How to sum arrays in php

PHPz
Release: 2023-04-18 13:59:35
Original
1077 people have browsed it

In PHP, it is very convenient to perform sum operations on arrays. Below we will introduce the summing method of PHP arrays in detail.

1. Use array_sum function to sum

PHP’s built-in array_sum function can sum all the values ​​in the array. Its syntax format is as follows:

array_sum(array $array): float
Copy after login

Among them, $array represents the array to be summed, and the return value is the summation result. It should be noted that array_sum can only sum elements whose values ​​are numbers. If there are non-numeric elements in the array, 0 will be returned.

The following is an example that demonstrates how to use the array_sum function to sum an array:

$numbers = array(1, 2, 3, 4, 5);
$total = array_sum($numbers);
echo "数组的和为:$total";
Copy after login

The running result is:

数组的和为:15
Copy after login
Copy after login
Copy after login
Copy after login

2. Use foreach loop summation

In addition to using PHP's built-in array_sum function, we can also use the foreach loop to accumulate the values ​​​​in the array one by one. The specific method is:

  1. Define a variable to save the summation result.
  2. Use a foreach loop to traverse the elements in the array one by one.
  3. Add the value of each element to the sum variable in the loop.

The following is an example that demonstrates how to use a foreach loop to sum an array:

$numbers = array(1, 2, 3, 4, 5);
$total = 0;
foreach ($numbers as $number) {
    $total += $number;
}
echo "数组的和为:$total";
Copy after login

The running result is:

数组的和为:15
Copy after login
Copy after login
Copy after login
Copy after login

3. Use a while loop to sum

In addition to using the foreach loop, we can also use the while loop to sum the array. The specific method is:

  1. Define a variable to save the summation result.
  2. Define a variable to save the current array index.
  3. Use a while loop to traverse the elements in the array one by one.
  4. Add the value of each element to the sum variable in the loop.
  5. Update the value of the current array subscript to continue looping.

The following is an example that demonstrates how to use a while loop to sum an array:

$numbers = array(1, 2, 3, 4, 5);
$total = 0;
$index = 0;
while ($index < count($numbers)) {
    $total += $numbers[$index];
    $index++;
}
echo "数组的和为:$total";
Copy after login

The running result is:

数组的和为:15
Copy after login
Copy after login
Copy after login
Copy after login

4. Use recursive functions to sum

Finally, we can also use recursive functions to sum multi-dimensional arrays. The specific method is:

  1. Define a variable to save the summation result.
  2. Use recursive functions to traverse multi-dimensional arrays.
  3. During the traversal process, if a non-array element is encountered, its value is accumulated into the summation variable.
  4. If an array element is encountered, continue to call the recursive function to traverse its subarrays.

The following is an example that demonstrates how to use a recursive function to sum multi-dimensional arrays:

function array_sum_recursive($array)
{
    $total = 0;
    foreach ($array as $element) {
        if (is_array($element)) {
            $total += array_sum_recursive($element);
        } else {
            $total += $element;
        }
    }
    return $total;
}

$numbers = array(1, 2, array(3, 4), 5);
$total = array_sum_recursive($numbers);
echo "数组的和为:$total";
Copy after login

The running result is:

数组的和为:15
Copy after login
Copy after login
Copy after login
Copy after login

The above is the PHP array sum There are several methods. You can choose the appropriate method to implement the array summation function according to your actual needs.

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

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