Estoy tratando de devolver una matriz "alterada".

Por ejemplo, mi matriz actual se ve así: El Código:

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

La matriz

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

  )

Ahora quiero contar los duplicados ([limit_lat]), eliminarlos de la matriz actual y volver a armarlos como la siguiente matriz:

así es como debo ver después de la purga duplicada.

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
        )   

    )

En este momento puedo contarlos muy bien, pero solo estoy recuperando esto: El Código:

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

La matriz:

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

Ya probé varios enfoques, pero no con el resultado deseado. Alguien tiene una idea de cómo puedo hacer que esto funcione?

Gracias Dennis

3
Dennis 10 sep. 2018 a las 17:57

3 respuestas

La mejor respuesta

Si limit_lat es todo lo que necesita, puede usar array_count_values y array_column.
Array_column filtra todos los elementos limit_lat en una matriz dimensional única, y array_count_values los contará.

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

Ahora $ count tendrá una salida similar a la salida esperada.

Editar: vea ahora que también desea eliminar los duplicados.

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

Esto debería funcionar, pero no puedo probarlo.
Si desea un código seguro, json_encode o var_export su matriz para que podamos usarlo.

2
Andreas 10 sep. 2018 a las 15:40

Puede seguir los "engaños" almacenándolos en una matriz separada, con los primeros elementos encontrados en otra matriz separada.

$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);
1
Mike Purcell 10 sep. 2018 a las 16:33

Puede excluirlos del bucle original comprobando si el valor único ya está en la matriz:

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

Su matriz filtrada de salida será indexada por la tecla limit_lat y contendrá una entrada $inBounds por valor limit_lat, y su matriz $usage contendrá el número de veces que {{ Se procesó el valor X4}}.

0
Robbie Averill 10 sep. 2018 a las 15:07