一尘不染

php使用多维数组按SUM分组

php

我有一个这样的PHP数组:

 Array
    (
        [0] => Array
            (
                [url_id] => 2191238
                [time_spent] => 41

            )

        [1] => Array
            (
                [url_id] => 2191606
                [time_spent] => 215

            )
        [2] => Array
            (
                [url_id] => 2191606
                [time_spent] => 25

            )
    )

因此,如何time_spent通过url_id 获取基于组的SUM(使用array_count_values)


阅读 381

收藏
2020-05-29

共1个答案

一尘不染

为什么不更简单

$ts_by_url = array();
foreach($array as $data) {
    $ts_by_url[ $data['url_id'] ] += $data['time_spent'];
}
2020-05-29