将二维数组中的行数据按一列进行分组,并对每组的另一列进行求和,然后找到总数最高的组[重复]

问题描述 投票:0回答:5

我正在尝试将员工的分数合并到我的计划中。我的样本是一名员工被评估超过一名。所以基本上,如果员工 1 的得分为 87 和 90,那么总分应该是 177。计算后,我需要返回得分最高的结果。 到目前为止,我的示例代码如下(我简化了这个问题的代码,因为发布原始代码太长了):

$scores = array( 
    array(1, "allan", 90),
    array(2, "allan", 85),
    array(3, "mark", 100),
    array(4, "jason", 88),
    array(5, "allan", 92),
    array(6, "mark", 77),
    array(7, "mark", 88),
    array(8, "jason", 90)
); 

print_r($scores);

get_topemployee($scores); 

function get_topemployee($scores) {
    $total_score = 0;
    $combined_score = array();

    foreach($scores as $key) {
        for($i=0; count($scores) <= $i; $i++) {
            if($key[0] == $key[0][$i]) {
                $total_score += $key[1];
            }
            $combined_score[] = array($key[0], $key[1], $total_score);
        }
    }

    $employee = ""; // employee with highest score
    $compare_score = 0;
    foreach($combined_score as $value) {
        $compare_score = $value[1];
        if($value[1] >= $compare_score) {
            $employee = $value[0];
        }
    }

    return $employee;
}

结果没有返回得分最高的员工,并且不确定我的代码中的哪个是不正确的。谢谢你的帮助

执行此操作:- https://3v4l.org/WhVaN

php arrays multidimensional-array sum grouping
5个回答
2
投票

您可以使用

array_reduce
将分数数组转换为按员工索引的总分数数组:

$scores = array_reduce($scores, function ($c, $v) { 
    $name = $v[1];
    if (isset($c[$name])) {
        $c[$name] += $v[2];
    }
    else {
        $c[$name] = $v[2];
    }
    return $c;
}, array());

然后您可以使用

arsort
以相反的顺序对数组进行排序,同时保持键关联。然后,您可以使用
key
获取第一个元素的键,这将是得分最高的员工的姓名:

arsort($scores);
echo key($scores);

输出:

allan

3v4l.org 上的演示

要打印所有分数,您可以简单地

print_r($scores)
:

Array (
  [allan] => 267
  [mark] => 265
  [jason] => 178 
)

或者要仅打印最高分,请将

echo key($scores);
更改为
echo key($scores) . ' ' . current($scores);

allan 267

更新了演示


2
投票

您可以使用简单的

foreach
循环来做到这一点:

foreach($scores as $e) {
    if (!isset($res[$e[1]])) $res[$e[1]] = 0;
    $res[$e[1]] += $e[2]; 
}
print_r(array_keys($res, max($res)));

实例:3v4l


2
投票

这是给您的片段,请参阅内联文档以获取解释

$result = [];
array_walk($scores, function($item) use(&$result){
   // pushing on the behalf of name and checking if not isset with '??'
   $result[$item[1]] = ($result[$item[1]] ?? 0) + $item[2]; 
});
// searching for index with max value in result and show max value too 
echo array_search(max($result), $result).' -> '. max($result);

array_search — 在数组中搜索给定值,如果成功则返回第一个对应的键
array_walk — 将用户提供的函数应用于数组的每个成员

输出

allan -> 267

演示链接.


1
投票
$scores = [
    [1, 'allan', 90],
    [2, 'allan', 85],
    [3, 'mark', 100],
    [4, 'jason', 88],
    [5, 'allan', 92],
    [6, 'mark', 77],
    [7, 'mark', 88],
    [8, 'jason', 90]
];

function maxScore(array $scores): string {
    $preparedScores = [];

    foreach ($scores as $score) {
        $key   = $score[1];
        $value = $score[2];

        if (isset($preparedScores[$key])) {
            $preparedScores[$key] += $value;
        } else {
            $preparedScores[$key] = $value;
        }
    }

    arsort($preparedScores);

    return array_key_first($preparedScores);
}

echo maxScore($scores);

结果:

allan

0
投票
$scores = array( 
    array(1, "allan", 90),
    array(2, "allan", 85),
    array(3, "mark", 100),
    array(4, "jason", 88),
    array(5, "allan", 92),
    array(6, "mark", 77),
    array(7, "mark", 88),
    array(8, "jason", 90)
); 


get_topemployee($scores); 

function get_topemployee($scores) {
    $total_score = 0;
    $combined_score = array();

    $highest_score = 0;
    $highest_employee = '';

    foreach($scores as $key => $value) {
        if ( ! isset($combined_score[$value[1]])) {
            $combined_score[$value[1]] = $value[2];
        }
        else {
            $combined_score[$value[1]] += $value[2];
        }

        if ($highest_score < $combined_score[$value[1]]) {
            $highest_score = $combined_score[$value[1]];
            $highest_employee = $value[1];  //the highest value employee
        }   
    }

    return $highest_employee;
}
© www.soinside.com 2019 - 2024. All rights reserved.