Dennis
Dennis

Reputation: 59

count same values in array and combine into array

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

Upvotes: 3

Views: 78

Answers (3)

Mike Purcell
Mike Purcell

Reputation: 19979

You could just keep track of the "dupes" by storing them in a separate array, with first found elements in yet another separate array.

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

Upvotes: 1

Andreas
Andreas

Reputation: 23958

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.

Upvotes: 2

scrowler
scrowler

Reputation: 24406

You could exclude them from the original loop by checking whether the unique value is already in the array:

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

Your output filtered array will be indexed by the limit_lat key and will contain one $inBounds entry per limit_lat value, and your $usage array will contain the number of times a limit_lat value was processed.

Upvotes: 0

Related Questions