Group similar elements of an Array in PHP

2

I'm working on a project for my company where I need to group elements of an array that have values in common. My array is as follows:

Array
(
    [0] => stdClass Object
        (
            [strRespAciona] => ALDREYACI
            [strMotivo] => Promessa de Pagamento
            [qtde] => 22
        )

    [1] => stdClass Object
        (
            [strRespAciona] => ALDREYACI
            [strMotivo] => Texto
            [qtde] => 53
        )

    [2] => stdClass Object
        (
            [strRespAciona] => BRENDAACI
            [strMotivo] => Promessa de Pagamento
            [qtde] => 6
        )

    [3] => stdClass Object
        (
            [strRespAciona] => BRENDAACI
            [strMotivo] => Texto
            [qtde] => 10
        )

    [4] => stdClass Object
        (
            [strRespAciona] => ELAINE
            [strMotivo] => NATE
            [qtde] => 1
        )

    [5] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => Não Localizado
            [qtde] => 5
        )

    [6] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => NATE
            [qtde] => 177
        )

    [7] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => OCUP
            [qtde] => 1
        )

    [8] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => Promessa de Pagamento
            [qtde] => 106
        )

    [9] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => Recado
            [qtde] => 218
        )

    [10] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => Sem Identificação
            [qtde] => 12
        )

    [11] => stdClass Object
        (
            [strRespAciona] => ELAINEACI
            [strMotivo] => Texto
            [qtde] => 1
        )

    [12] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => FALE
            [qtde] => 1
        )

    [13] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => Não Localizado
            [qtde] => 922
        )

    [14] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => NATE
            [qtde] => 1
        )

    [15] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => Promessa de Pagamento
            [qtde] => 241
        )

    [16] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => Recado
            [qtde] => 444
        )

    [17] => stdClass Object
        (
            [strRespAciona] => JHONATANAC
            [strMotivo] => Texto
            [qtde] => 226
        )

    [18] => stdClass Object
        (
            [strRespAciona] => MONIQUEACI
            [strMotivo] => FALE
            [qtde] => 2
        )

    [19] => stdClass Object
        (
            [strRespAciona] => MONIQUEACI
            [strMotivo] => Não Localizado
            [qtde] => 149
        )

    [20] => stdClass Object
        (
            [strRespAciona] => MONIQUEACI
            [strMotivo] => Promessa de Pagamento
            [qtde] => 100
        )

    [21] => stdClass Object
        (
            [strRespAciona] => MONIQUEACI
            [strMotivo] => Recado
            [qtde] => 211
        )

    [22] => stdClass Object
        (
            [strRespAciona] => MONIQUEACI
            [strMotivo] => Texto
            [qtde] => 83
        )

    [23] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => ENEG
            [qtde] => 1
        )

    [24] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => FALE
            [qtde] => 1
        )

    [25] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => Não Localizado
            [qtde] => 765
        )

    [26] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => Promessa de Pagamento
            [qtde] => 221
        )

    [27] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => Recado
            [qtde] => 394
        )

    [28] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => Sem Identificação
            [qtde] => 2
        )

    [29] => stdClass Object
        (
            [strRespAciona] => RAFAELACI
            [strMotivo] => Texto
            [qtde] => 403
        )

    [30] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => ENEG
            [qtde] => 2
        )

    [31] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => FALE
            [qtde] => 1
        )

    [32] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => LIGD
            [qtde] => 7
        )

    [33] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => Não Localizado
            [qtde] => 962
        )

    [34] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => Promessa de Pagamento
            [qtde] => 372
        )

    [35] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => Recado
            [qtde] => 606
        )

    [36] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => TELM
            [qtde] => 3
        )

    [37] => stdClass Object
        (
            [strRespAciona] => TAMILLESAC
            [strMotivo] => Texto
            [qtde] => 280
        )

)

I need to group the items by the key strRespAciona , so that it looks similar to this:

[ALDREYACI]=> 
(
        ['Promessa de Pagamento'] => 22
        ['Texto']=> 53
) 

What is the best way to get a similar result?

Edit: There is some more information I have to work with. Along with this array, I get two more: one with all possible values of the strMotivo key and the other with all possible values of the strRespAciona key. The possible values for the strRespAciona key are:

Array
(
    [0] => BRENDAACI
    [1] => MONIQUEACI
    [2] => ELAINEACI
    [3] => ELAINE
    [4] => JHONATANAC
    [5] => TAMILLESAC
    [6] => RAFAELACI
    [7] => ALDREYACI
)

And the possible values for the strMotivo key are:

Array
(
    [0] => ENEG
    [1] => FALE
    [2] => LIGD
    [3] => Não Localizado
    [4] => NATE
    [5] => OCUP
    [6] => Promessa de Pagamento
    [7] => Recado
    [8] => Sem Identificação
    [9] => TELM
    [10] => Texto
)

So far, I have this code:

            $data = $http_response->data;
            $motivos = $http_response->motivos;
            $agentes = $http_response->agentes;

            $data = json_decode(json_encode($data), true);

            $json = [];

            for ($a = 0; $a < count($agentes); $a++) {
                $json[$a]['nome'] = $agentes[$a];
                for ($m = 0; $m < count($motivos); $m++) {
                    for ($d = 0; $d < count($data); $d++) {
                        if ($agentes[$a] == $data[$d]['strMotivo']) {
                            $json[$a][$motivos[$m]] = $data[$d]['qtde'];
                        } else {
                            $json[$a][$motivos[$m]] = 0;
                        }
                    }
                }
            }

The variables $ data, $ motifs, and $ agents receive application API data, which in turn extracts them from the enterprise database. This is the current result of the above code:

Array
(
    [0] => Array
        (
            [nome] => TAMILLESAC
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [1] => Array
        (
            [nome] => JHONATANAC
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [2] => Array
        (
            [nome] => ELAINE
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [3] => Array
        (
            [nome] => BRENDAACI
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [4] => Array
        (
            [nome] => MONIQUEACI
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [5] => Array
        (
            [nome] => ELAINEACI
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [6] => Array
        (
            [nome] => RAFAELACI
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

    [7] => Array
        (
            [nome] => ALDREYACI
            [ENEG] => 0
            [FALE] => 0
            [LIGD] => 0
            [Não Localizado] => 0
            [NATE] => 0
            [OCUP] => 0
            [Promessa de Pagamento] => 0
            [Recado] => 0
            [Sem Identificação] => 0
            [TELM] => 0
            [Texto] => 0
        )

)

While expected is that the keys where they correspond in the first array of my doubt have the values corresponding to them and the other values are filled with zero.

    
asked by anonymous 12.06.2018 / 20:25

1 answer

1

Based on your scenario, it could be something like

<?php

$array = array(
    array( "strRespAciona" => "mauro", 2 ),
    array( "strRespAciona" => "mauro", 1 ),
    array( "strRespAciona" => "ellaa", 3 )
);

$data = [];

foreach($array as $elm){
    $data[ array_shift($elm) ][] = $elm;
}

print_r($data); //resultado abaixo

result:

Array
(
    [mauro] => Array
        (
            [0] => Array
                (
                    [0] => 2
                )

            [1] => Array
                (
                    [0] => 1
                )

        )

    [ellaa] => Array
        (
            [0] => Array
                (
                    [0] => 3
                )

        )

)

Summary

To group the data, it is necessary to iterate the array to get the elements $elm ; in the line $data[ array_shift($elm) ][] = $elm; the array_shift is responsible for correctly grouping the same data (removes the same elements, keeping only one and reads the variable $data ).

    
12.06.2018 / 20:43