简体   繁体   English

2D数组-按值分组并汇总相同键的值

[英]2d array - group by value and sum up same keys' values

Hello and thanks for taking the time to look at my question. 您好,感谢您抽出宝贵时间来查看我的问题。

My current problem is that, I have the following array ( $topics_percentage ): 我当前的问题是,我有以下数组( $topics_percentage ):

Array
(
    [0] => Array
        (
            [id] => 8989
            [cat] => Category 1
            [completed] => 0
        )

    [1] => Array
        (
            [id] => 8919
            [cat] => Category 2
            [completed] => 1
        )

    [2] => Array
        (
            [id] => 8913
            [cat] => Category 2
            [completed] => 1
        )

    [3] => Array
        (
            [id] => 8947
            [cat] => Category 1
            [completed] => 1
        )

    [4] => Array
        (
            [id] => 8949
            [cat] => Category 3
            [completed] => 1
        )

)

What I need to get, is something like the following example: 我需要得到的是类似以下示例的内容:

Array
(
    [Category 1] => Array
        (
            [noncompleted] => 1
            [completed] => 1
        )

    [Category 2] => Array
        (
            [completed] => 2
        )

    [Category 3] => Array
        (
            [completed] => 1
        )

)


What I've tried so far is this: 到目前为止,我尝试过的是:

$class_array = [];

foreach ($topics_percentage as $v) {

    $complete = $v['completed'];

    $class_array[$v['cat']][] =
        ($complete == 0 ? 'noncompleted' : 'completed');
}

Which returns the following: 返回以下内容:

Array
(
    [Category 1] => Array
        (
            [0] => noncompleted
            [1] => completed
        )

    [Category 2] => Array
        (
            [0] => completed
            [1] => completed
        )

    [Category 3] => Array
        (
            [0] => completed
        )

)

I'm weak when it comes to arrays, and can't figure out the logic. 对于数组,我很虚弱,无法弄清楚逻辑。

Thanks in advance. 提前致谢。

Try this: 尝试这个:

$values = array_unique(array_map(
    function ($v) { return $v['cat']; },
    $array
));

$result = array();
foreach ($values as $val) {
    $flt = array_filter($array, function ($v) use ($val) {
        return $v['cat'] == $val;
    });
    $cnt = count(array_filter($array, function ($v) use ($val) {
        return $v['completed'];
    }));
    $result[$val] = array(
        'completed' => $cnt,
        'noncompleted' => count($flt) - $cnt
    );
}

Try this: 尝试这个:

    foreach ($topics_percentage as $v) {

        $complete = $v['completed'];

        if ($complete == 1) {
            $class_array[$v['cat']]['completed']++;
        } else {
            $class_array[$v['cat']]['uncompleted']++;
        }
    }

You should, of course, preinitialize the array before you count up values, but the basic idea is to use array cells as numeric values to be added or substracted from. 当然,您应该在对值进行计数之前预先初始化数组,但是基本思想是使用数组单元作为要添加或减去的数字值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM