Home >Backend Development >PHP Tutorial >How to use PHP functions for data aggregation?

How to use PHP functions for data aggregation?

PHPz
PHPzOriginal
2024-05-03 21:57:021242browse

PHP provides functions for data aggregation, including: sum() Calculate the total count() Calculate the quantity max() and min() Find the maximum and minimum values ​​array_column() Extract the specified column from the array array_reduce() Application In the practical case of aggregate functions, an example of calculating the total score and the average score of each student is shown.

如何使用 PHP 函数进行数据聚合?

How to use PHP functions for data aggregation

Data aggregation is the combination of data points in data analysis to create a higher The process of level summarization. PHP provides several functions that help you aggregate data easily.

Calculate the sum using the sum() function

sum() The function adds all the numbers in an array and returns the result.

$numbers = [1, 2, 3, 4, 5];
$total = sum($numbers); // 15

Use the count() function to calculate the number

count() The function returns the number of elements in the array.

$names = ['John', 'Jane', 'Doe'];
$count = count($names); // 3

Find the maximum and minimum values ​​using the max() and min() functions

##max() and min() The function returns the maximum value and minimum value in the array respectively.

$scores = [90, 85, 95, 75];
$max = max($scores); // 95
$min = min($scores); // 75

Use the array_column() function to extract the specified column from the array

array_column() The function extracts the specified column from each array in the array columns and returns a one-dimensional array.

$data = [
    ['id' => 1, 'name' => 'John', 'score' => 90],
    ['id' => 2, 'name' => 'Jane', 'score' => 85],
    ['id' => 3, 'name' => 'Doe', 'score' => 95]
];

$scores = array_column($data, 'score'); // [90, 85, 95]

Use the array_reduce() function to apply an aggregate function

array_reduce() The function passes the elements in the array to an aggregate function one by one, and Return the final result.

$numbers = [1, 2, 3, 4, 5];
$total = array_reduce($numbers, function($carry, $item) {
  return $carry + $item;
}, 0); // 15

Actual case

$data = [
    ['id' => 1, 'name' => 'John', 'score' => 90],
    ['id' => 2, 'name' => 'Jane', 'score' => 85],
    ['id' => 3, 'name' => 'Doe', 'score' => 95]
];

// 计算总分
$total = array_reduce($data, function($carry, $item) {
  return $carry + $item['score'];
}, 0);

// 计算每个学生的平均分
$averages = array_map(function($item) {
  return $item['score'] / count($item);
}, $data);

// 输出结果
echo "总分: $total\n";
foreach ($averages as $id => $average) {
  echo "学生 {$id} 平均分: $average\n";
}

Output result:

总分: 270
学生 1 平均分: 90.0000
学生 2 平均分: 85.0000
学生 3 平均分: 95.0000

The above is the detailed content of How to use PHP functions for data aggregation?. 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