计算数组中的相同值,然后合并为数组

时间:2018-09-10 14:57:10

标签: php arrays

我正在尝试返回“更改后的”数组。

例如,我当前的数组如下所示: 代码:

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

数组

    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] => 
        )

  )

现在,我想计算重复项([limit_lat]),将其从当前数组中删除,然后像以下数组一样将它们放回一起:

这是重复清除后的外观。

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
        )   

    )

现在我能够很好地计数它们,但是我只能将其取回来: 代码:

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

数组:

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

我已经尝试了多种方法,但没有得到想要的结果。有人知道我如何使它工作?

谢谢, 丹尼斯

3 个答案:

答案 0 :(得分:2)

如果只需要limit_lat,则可以使用array_count_valuesarray_column
Array_column将所有limit_lat项过滤到一个维数组中,array_count_values将对其进行计数。

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

现在$ count将具有与预期输出类似的输出。

编辑:现在您也要删除重复项。

$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);

这应该可以,但是我无法测试。
如果您想要确定的代码json_encode或var_export您的数组,以便我们可以使用它。

答案 1 :(得分:1)

您可以通过将“重复项”存储在单独的数组中来跟踪“重复项”,将首先找到的元素存储在另一个单独的数组中。

$limitLats = [];
$trulyFiltered = [];

foreach($filtered as $cluster) {

    if (true === array_key_exists($cluster['limit_lat'], $trulyFiltered)) {

        $limitLats[$cluster['id']] = $cluster['limit_lat'];

        continue;
    }

    $trulyFiltered[$cluster['limit_lat']] = $cluster;
}

// Should give you the count for # of dupes
echo count($limitLats);

// Should give you unique dataset based on limit_lat
var_dump($trulyFiltered);

答案 2 :(得分:0)

您可以通过检查数组中是否已经存在唯一值,将它们从原始循环中排除:

$filtered = array();
$usage = array();

foreach ($inBounds as $index => $columns) {
    foreach ($columns as $key => $value) {       
        $uniqueKey = $columns['limit_lat'];
        // track use of unique key
        $usage[$uniqueKey]++;

        if (array_key_exists($uniqueKey, $filtered) {
            // already added one, skip
            continue;
        }
        $filtered[$uniqueKey] = $columns;
    }
}

您的输出过滤数组将通过limit_lat键索引,并且每个$inBounds值将包含一个limit_lat条目,而您的$usage数组将包含次数limit_lat值已处理。