计算多维对象数组中的重复值

时间:2019-01-08 21:30:26

标签: php laravel multidimensional-array eloquent duplicates

我有一个多维数组对象$ all2 [$ i] [$ j],但未定义,但通过我的laravel sql查询请求收到:

for($i=0; $i<(count($all));$i++){
            $uID = HashtagFollow::select('user_id')->where('hashtag_id', $all[$i]->hashtag_id)->where('user_id', '!=', $loggedin->id)->get();

    for($j=0;$j<count($uID);$j++){
$all2[$i][$j] = HashtagFollow::select('hashtag_id')->where('user_id', $uID[$j]->user_id)->whereNotIn('hashtag_id', $all)->orderBy('hashtag_id')->get();
}
}

给出以下输出:

0: [[{hashtag_id: 5}], [{hashtag_id: 5}, {hashtag_id: 13}], [{hashtag_id: 2}, {hashtag_id: 13}]]
    0: [{hashtag_id: 5}]
    1: [{hashtag_id: 5}, {hashtag_id: 13}]
    2: [{hashtag_id: 2}, {hashtag_id: 13}]

1: [[{hashtag_id: 5}, {hashtag_id: 13}], [{hashtag_id: 5}], [{hashtag_id: 5}],…]
    0: [{hashtag_id: 5}, {hashtag_id: 13}]
    1: [{hashtag_id: 5}]
    2: [{hashtag_id: 5}]
    3: [{hashtag_id: 12}, {hashtag_id: 13}]

2: [[{hashtag_id: 5}, {hashtag_id: 13}], [{hashtag_id: 5}], [{hashtag_id: 12}, {hashtag_id: 13}]]
    0: [{hashtag_id: 5}, {hashtag_id: 13}]
    1: [{hashtag_id: 5}]
    2: [{hashtag_id: 12}, {hashtag_id: 13}]

3: [[{hashtag_id: 5}, {hashtag_id: 13}], [{hashtag_id: 2}, {hashtag_id: 12}, {hashtag_id: 13}],…]
    0: [{hashtag_id: 5}, {hashtag_id: 13}]
    1: [{hashtag_id: 2}, {hashtag_id: 12}, {hashtag_id: 13}]
    2: [{hashtag_id: 5}]
    3: [{hashtag_id: 5}, {hashtag_id: 13}]
    4: [{hashtag_id: 2}, {hashtag_id: 13}]
    5: [{hashtag_id: 12}, {hashtag_id: 13}]

4: [[{hashtag_id: 5}, {hashtag_id: 13}], [{hashtag_id: 5}], [{hashtag_id: 5}],…]
    0: [{hashtag_id: 5}, {hashtag_id: 13}]
    1: [{hashtag_id: 5}]
    2: [{hashtag_id: 5}]
    3: [{hashtag_id: 5}, {hashtag_id: 13}]
    4: [{hashtag_id: 12}, {hashtag_id: 13}]

5: [[{hashtag_id: 5}], [{hashtag_id: 2}, {hashtag_id: 12}, {hashtag_id: 13}],…]
    0: [{hashtag_id: 5}]
    1: [{hashtag_id: 2}, {hashtag_id: 12}, {hashtag_id: 13}]
    2: [{hashtag_id: 5}, {hashtag_id: 12}, {hashtag_id: 13}]
    3: [{hashtag_id: 5}]
    4: [{hashtag_id: 2}, {hashtag_id: 13}]
    5: [{hashtag_id: 12}, {hashtag_id: 13}]

我现在正在尝试循环此对象数组,并计算每个hashtag_id中有多少。

预期输出: hashtag_id:2计数:2 hashtag_id:5计数:5 hashtag_id:12计数:3 hashtag_id:13计数:6

1 个答案:

答案 0 :(得分:0)

您可能要考虑直接查询这些计数。

例如

SELECT hashtag_id, user_id, COUNT(*) AS count FROM HashtagFollow GROUP BY hashtag_id, user_id;