
在数据处理中,我们经常会遇到需要对结构相似但存储在不同并行数组中的数据进行聚合的场景。例如,有一组交易状态数组和对应的交易数量、收入、毛利数组,我们希望按交易状态对这些数据进行分组,并计算每个状态的总交易数量、总收入和总毛利。本文将深入探讨几种在 php 中实现这一目标的高效方法。
假设我们有以下并行数组,其中 $statuses 数组定义了分组的依据:
$statuses = ['PROSPECT', 'BACKLOG', 'PROSPECT']; $of_tranxs = [2, 1, 2]; // 交易数量 $revs = [3, 1, 3]; // 收入 $mgps = [4, 1, 4]; // 毛利
我们的目标是根据 $statuses 数组中的值进行分组,并对 $of_tranxs、$revs 和 $mgps 数组中对应位置的数值进行求和。期望的输出格式如下:
array( 'status' => ['PROSPECT', 'BACKLOG'], 'of_tranx' => [4, 1], // PROSPECT: 2+2=4, BACKLOG: 1 'rev' => [6, 1], // PROSPECT: 3+3=6, BACKLOG: 1 'mgp' => [8, 1] // PROSPECT: 4+4=8, BACKLOG: 1 )
接下来,我们将介绍三种不同的实现方法。
这种方法通过在原始数组上直接进行操作来聚合数据。它利用一个辅助数组来记录每个状态第一次出现时的索引,后续遇到重复状态时,将当前数据累加到第一次出现的位置,并删除当前位置的数据。最后,通过 array_values() 重新索引以获得紧凑的数组。
立即学习“PHP免费学习笔记(深入)”;
<?php
$statuses = ['PROSPECT', 'BACKLOG', 'PROSPECT'];
$of_tranxs = [2, 1, 2];
$revs = [3, 1, 3];
$mgps = [4, 1, 4];
$found = []; // 用于记录每个状态第一次出现的索引
foreach ($statuses as $index => $status) {
if (!isset($found[$status])) {
// 如果是第一次遇到该状态,记录其索引
$found[$status] = $index;
continue;
}
// 如果该状态已存在,将当前值累加到第一次出现的索引位置
$of_tranxs[$found[$status]] += $of_tranxs[$index];
$revs[$found[$status]] += $revs[$index];
$mgps[$found[$status]] += $mgps[$index];
// 删除当前重复状态及其对应的数据
unset($statuses[$index], $of_tranxs[$index], $revs[$index], $mgps[$index]);
}
// 重新索引数组以消除被删除元素留下的空洞
$result = [
'status' => array_values($statuses),
'of_tranx' => array_values($of_tranxs),
'rev' => array_values($revs),
'mgp' => array_values($mgps)
];
var_export($result);
?>array (
'status' =>
array (
0 => 'PROSPECT',
1 => 'BACKLOG',
),
'of_tranx' =>
array (
0 => 4,
1 => 1,
),
'rev' =>
array (
0 => 6,
1 => 1,
),
'mgp' =>
array (
0 => 8,
1 => 1,
),
)这种方法通过构建一个全新的结果数组来避免修改原始数据,并显式地管理新数组中的索引。
<?php
$statuses = ['PROSPECT', 'BACKLOG', 'PROSPECT'];
$of_tranxs = [2, 1, 2];
$revs = [3, 1, 3];
$mgps = [4, 1, 4];
$result = [];
$newIndex = []; // 映射状态到新结果数组中的索引
$i = 0; // 新结果数组的索引计数器
foreach ($statuses as $oldIndex => $status) {
if (!isset($newIndex[$status])) {
// 如果是第一次遇到该状态,在新结果数组中添加新条目
$newIndex[$status] = $i++; // 记录新索引并递增
$result['status'][] = $status;
$result['of_tranx'][] = $of_tranxs[$oldIndex];
$result['rev'][] = $revs[$oldIndex];
$result['mgp'][] = $mgps[$oldIndex];
} else {
// 如果状态已存在,累加到对应的新索引位置
$targetNewIndex = $newIndex[$status];
$result['of_tranx'][$targetNewIndex] += $of_tranxs[$oldIndex];
$result['rev'][$targetNewIndex] += $revs[$oldIndex];
$result['mgp'][$targetNewIndex] += $mgps[$oldIndex];
}
}
var_export($result);
?>array (
'status' =>
array (
0 => 'PROSPECT',
1 => 'BACKLOG',
),
'of_tranx' =>
array (
0 => 4,
1 => 1,
),
'rev' =>
array (
0 => 6,
1 => 1,
),
'mgp' =>
array (
0 => 8,
1 => 1,
),
)这种方法在构建新数组的同时,利用 PHP 的引用机制来简化代码,并能生成更具结构化的输出(例如,一个包含关联数组的数组)。
<?php
$statuses = ['PROSPECT', 'BACKLOG', 'PROSPECT'];
$of_tranxs = [2, 1, 2];
$revs = [3, 1, 3];
$mgps = [4, 1, 4];
$result = [];
$ref = []; // 存储每个状态的聚合数据,并通过引用连接到 $result
foreach ($statuses as $i => $status) {
if (!isset($ref[$status])) {
// 如果是第一次遇到该状态,创建新的聚合数据结构
$ref[$status] = [
'status' => $status,
'of_tranx' => $of_tranxs[$i],
'rev' => $revs[$i],
'mgp' => $mgps[$i],
];
// 将该聚合数据的引用推入结果数组
$result[] = &$ref[$status];
} else {
// 如果状态已存在,通过引用直接更新聚合数据
$ref[$status]['of_tranx'] += $of_tranxs[$i];
$ref[$status]['rev'] += $revs[$i];
$ref[$status]['mgp'] += $mgps[$i];
}
}
var_export($result);
?>array (
0 =>
array (
'status' => 'PROSPECT',
'of_tranx' => 4,
'rev' => 6,
'mgp' => 8,
),
1 =>
array (
'status' => 'BACKLOG',
'of_tranx' => 1,
'rev' => 1,
'mgp' => 1,
),
)本文介绍了三种在 PHP 中根据一个数组对多个并行数组进行分组和求和的有效方法。
选择哪种方法取决于具体的业务需求、对性能的考量以及对代码可读性和维护性的偏好。所有这些方法都通过单次遍历实现了数据聚合,因此在处理大量数据时都具有较好的性能表现。在 PHP 7.4 及更高版本中,这些方法均可稳定运行。
以上就是PHP 数据聚合:根据一个数组对多个并行数组进行分组和求和的详细内容,更多请关注php中文网其它相关文章!
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号