Group similar elements of an Array in PHP

Asked

Viewed 134 times

2

I am 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 key strRespAciona, to make it similar to this:

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

What is the best way to get a similar result to the expected?

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

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

And the possible values for the key strMotivo sane:

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 $data, $reasons and $agents variables receive data from the application’s API, which in turn extracts it from the company’s 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 waiting is that the keys where there are matches in the first array of my doubt have the values corresponding to them and the other values are filled with zero.

  • What have you tried to do? If you ask for the best form, I believe you have already achieved the expected result. If not, what was the difficulty?

  • I believe I expressed myself badly. I can get almost where I need to get. I can group the keys, but not the quantities. The difficulty is in precisely grouping all this information as needed.

  • So add that code to the question, so we don’t have to start from scratch.

  • I added more information to the question, now I believe it is more complete.

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

outworking:

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

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

        )

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

        )

)

Summary

To group the data, you need to iterate the array to get the elements $elm; on the line $data[ array_shift($elm) ][] = $elm; the array_shift is responsible for correctly grouping the equal data (removes the equal elements, keeping only one and readjusts them to variable $data).

  • Apparently your code can solve my problem, but you can explain to me how it works?

  • After all, it was after seeing your code that I was able to solve the problem. Thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.