Home  >  Article  >  Backend Development  >  How to sum a value in a one-dimensional array in PHP

How to sum a value in a one-dimensional array in PHP

PHPz
PHPzOriginal
2023-04-23 19:29:51798browse

In PHP, array is a very commonly used data type, and the summation operation of a specified value in the array is also one of the common requirements. This article will introduce how to use PHP to sum a value in a one-dimensional array, and provide some related examples as a reference.

1. Use the array_reduce function to implement summation

The array_reduce function can reduce all elements in the array, and the reduction method can be defined by yourself. When a certain attribute in the array needs to be summed, the attribute can be used as a parameter of the reduction operation. The specific implementation is as follows:

$numbers = array(10, 20, 30, 40, 50);
$sum = array_reduce($numbers, function($carry, $item) {
    return $carry + $item;
}, 0);
echo "数组总和为:" . $sum;

In the above code, we first define an array$numbers, and use the array_reduce function to perform the sum operation. In the reduction operation, we use the $carry variable to save the result after the last reduction, and the $item variable represents the value of the current element. In this example, the reduction operation is a simple addition operation, and 0 represents the initial value of the first reduction operation.

Run the above code, we can get the following results:

数组总和为:150

The above example demonstrates how to use the array_reduce function to solve the sum of elements in a one-dimensional array. But if we want to sum an attribute in a one-dimensional array, how should we do it? At this time, a callback function can be used as a parameter of the reduction operation, and the reduction attributes can be filtered and added in the callback function. The specific implementation is as follows:

$students = array (
    "Tom" => array("age" => 23, "score" => 80),
    "Jerry" => array("age" => 22, "score" => 90),
    "Mickey" => array("age" => 23, "score" => 85)
);

$sum = array_reduce($students, function($carry, $item) {
    return $carry + $item['score'];
}, 0);
echo "学生成绩总分为:" . $sum;

In the above code, we define an array of student information $students, where each element consists of a name, age and score. We use the array_reduce function and a callback function to accumulate the scores of each student element to get the total score of the student's grade.

Running the above code, we can get the following results:

学生成绩总分为:255

As can be seen from the above example, the array_reduce function can be used to flexibly implement the summation operation of a certain attribute in a one-dimensional array.

2. Use the foreach loop to implement summation

Compared with the array_reduce function, PHP can also use the regular foreach loop to implement the summation of a certain attribute in a one-dimensional array. The specific implementation is as follows:

$students = array (
    "Tom" => array("age" => 23, "score" => 80),
    "Jerry" => array("age" => 22, "score" => 90),
    "Mickey" => array("age" => 23, "score" => 85)
);

$sum = 0;
foreach ($students as $student) {
    $sum += $student['score'];
}
echo "学生成绩总分为:" . $sum;

The implementation here is relatively simple, that is, using a foreach loop to traverse all the elements in the array in sequence, and accumulate the scores of each element, and finally obtain the total score of the student's performance.

Run the above code, we can get the following results:

学生成绩总分为:255

It can be seen that using foreach loop is also a common implementation method, especially suitable for processing smaller arrays.

3. Use the array_sum function to implement summation

In addition to the array_reduce function, PHP also provides an array_sum function specifically used to solve the sum of array elements. This function is very simple. It just takes an array as a parameter and returns the sum of its elements. The specific implementation is as follows:

$numbers = array(10, 20, 30, 40, 50);
$sum = array_sum($numbers);
echo "数组总和为:" . $sum;

In the above code, we call the array_sum function to sum the array$numbers operation, and finally get its sum.

Run the above code, we can get the following results:

数组总和为:150

It should be noted that the array_sum function can only be used to solve one-dimensional digital index arrays. If you need to calculate a certain value in a one-dimensional associative array To sum an attribute, you need to first use the array_column function to convert it into a numerical index array, and then use the array_sum function to perform the sum.

4. Summary

This article introduces the implementation method of summing an attribute in a one-dimensional array in PHP, including the array_reduce function, foreach loop and array_sum function. In comparison, the array_reduce function is the most flexible and can adapt to various scenarios, but its complexity is also relatively high. The foreach loop and array_sum function are simpler and more straightforward, suitable for processing small arrays. Different implementation methods can be selected according to actual needs to achieve optimal results.

The above is the detailed content of How to sum a value in a one-dimensional array 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