douguazhi5966 2018-09-10 14:57
浏览 24
已采纳

计算数组中的相同值并组合成数组

i am trying to return a "altered" array.

for example my current array looks like this: The Code:

    $filtered = array();
foreach($inBounds as $index => $columns) {
    foreach($columns as $key => $value) {       
    if(in_array($columns,$filtered)){
    }else{
    $filtered[$z] = $columns;
    $z = $z + 1;    
    }
}
}

The Array

    Array
     (
        [141] => Array
            (
                [id] => 1006
                [lat] => 51.28940600
                [lng] => 6.98730500
                [name] => fghfgh
                [date] => 2018-08-31 11:47:23
                [sizeIcon] => icon1
                [limit_lat] => 51
                [count] => 
            )

        [142] => Array
            (
                [id] => 1007
                [lat] => 51.29198200
                [lng] => 6.97700500
                [name] => asdasd
                [date] => 2018-08-31 13:55:11
                [sizeIcon] => icon1
                [limit_lat] => 51
                [count] => 
            )

        [143] => Array
            (
                [id] => 1008
                [lat] => 51.27308500
                [lng] => 6.97563200
                [name] => adasdsad
                [date] => 2018-08-31 13:55:16
                [sizeIcon] => icon1
                [limit_lat] => 51
                [count] => 
            )

        [144] => Array
            (
                [id] => 1009
                [lat] => 51.97811300
                [lng] => 7.83325200
                [name] => wer
                [date] => 2018-08-31 13:56:02
                [sizeIcon] => icon1
                [limit_lat] => 52
                [count] => 
            )

        [145] => Array
            (
                [id] => 1010
                [lat] => 51.92394300
                [lng] => 8.60229500
                [name] => werwer
                [date] => 2018-08-31 13:56:07
                [sizeIcon] => icon1
                [limit_lat] => 52
                [count] => 
            )

        [146] => Array
            (
                [id] => 1011
                [lat] => 27.95195200
                [lng] => -82.46612500
                [name] => sdfsdf
                [date] => 2018-08-31 13:57:12
                [sizeIcon] => icon1
                [limit_lat] => 28
                [count] => 
            )

        [147] => Array
            (
                [id] => 1012
                [lat] => 27.94588600
                [lng] => -82.42080700
                [name] => sdfsdfsdf
                [date] => 2018-08-31 13:57:16
                [sizeIcon] => icon1
                [limit_lat] => 28
                [count] => 
            )

        [148] => Array
            (
                [id] => 1013
                [lat] => 28.00773900
                [lng] => -82.48672500
                [name] => werwerwer
                [date] => 2018-08-31 13:57:20
                [sizeIcon] => icon1
                [limit_lat] => 28
                [count] => 
            )

        [149] => Array
            (
                [id] => 1014
                [lat] => 28.11438300
                [lng] => -82.43454000
                [name] => bvbvbvb
                [date] => 2018-08-31 13:57:31
                [sizeIcon] => icon1
                [limit_lat] => 28
                [count] => 
            )
    [140] => Array
        (
            [id] => 1005
            [lat] => 49.48240100
            [lng] => 0.28564500
            [name] => dsfsdf
            [date] => 2018-08-30 15:21:32
            [sizeIcon] => icon1
            [limit_lat] => 49
            [count] => 
        )

  )

Now i want to count the duplicates ( [limit_lat] ), remove them from the current array and put them back together like the following array:

this is what i need it to look like after the duplicate purge.

Array
 (
    [0] => Array
        (
            [id] => 1006
            [lat] => 51.28940600
            [lng] => 6.98730500
            [name] => fghfgh
            [date] => 2018-08-31 11:47:23
            [sizeIcon] => icon1
            [limit_lat] => 51
            [count] => 3
        )


    [1] => Array
        (
            [id] => 1009
            [lat] => 51.97811300
            [lng] => 7.83325200
            [name] => wer
            [date] => 2018-08-31 13:56:02
            [sizeIcon] => icon1
            [limit_lat] => 52
            [count] => 2
        )

    [2] => Array
        (
            [id] => 1011
            [lat] => 27.95195200
            [lng] => -82.46612500
            [name] => sdfsdf
            [date] => 2018-08-31 13:57:12
            [sizeIcon] => icon1
            [limit_lat] => 28
            [count] => 4
        )
    [3] => Array
        (
            [id] => 1005
            [lat] => 49.48240100
            [lng] => 0.28564500
            [name] => dsfsdf
            [date] => 2018-08-30 15:21:32
            [sizeIcon] => icon1
            [limit_lat] => 49
            [count] => 1
        )   

    )

right now i am able to count them nicely but i am only getting this back: The Code:

$data = array();
foreach($filtered as $cluster) {
    if(isset($data[$cluster['limit_lat']])) {
        $data[$cluster['limit_lat']]++;
    } else {
        $data[$cluster['limit_lat']] = 1;
    }
}

The Array:

Array
(
    [51] => 3
    [52] => 2
    [28] => 4
    [49] => 1
)

I already tried multiple approaches but not with the wanted result. Someone has an idea how i can get this working?

Thanks, Dennis

  • 写回答

3条回答 默认 最新

  • doujiaozhan4397 2018-09-10 15:18
    关注

    If limit_lat is all you need you can use array_count_values and array_column.
    Array_column filters out all limit_lat items to a single dimensional array, and array_count_values will count them.

    $count = array_count_values(array_column($arr, "limit_lat"));
    

    Now $count will have a similar output as your expected output.

    Edit: see now that you want to remove duplicates too.

    $count = array_count_values(array_column($arr, "limit_lat"));
    // Remove duplicates
    $nodupes = array_column($arr, Null, "limit_lat");
    
    // Add count to array items.
    foreach ($count as $key => $c){
         $nodupes[$key]['count'] = $c;
    }
    $nodupes = array_values($nodupes);
    

    This should work, but I can't test it.
    If you want a sure code json_encode or var_export your array so that we can use it.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度