Return Number of Responses PHP Array

Asked

Viewed 42 times

2

I have the following array():

Array
(
    [0] => stdClass Object
        (
            [men_id] => 3
            [usu_id] => 2
            [usu_destino] => 2
            [id_resposta] => 0
            [usu_respondeu] => 0
            [set_assunto] => 147
            [men_mensagem] => Tese 03
            [men_data] => 2017-11-05 16:02:39
            [men_data_lida] => 0000-00-00 00:00:00
            [men_resposta] => 1
            [men_status] => 1
            [total_respostas] => 1
        )

    [1] => stdClass Object
        (
            [men_id] => 21
            [usu_id] => 2
            [usu_destino] => 3
            [id_resposta] => 0
            [usu_respondeu] => 0
            [set_assunto] => 147
            [men_mensagem] => Teste de Envio de Mensagem
            [men_data] => 2017-11-05 19:30:38
            [men_data_lida] => 0000-00-00 00:00:00
            [men_resposta] => 1
            [men_status] => 1
            [total_respostas] => 0
        )

    [2] => stdClass Object
        (
            [men_id] => 22
            [usu_id] => 2
            [usu_destino] => 2
            [id_resposta] => 0
            [usu_respondeu] => 0
            [set_assunto] => 147
            [men_mensagem] => Apenas enviar um email para o dom Jose da Silva, [email protected]
            [men_data] => 2017-11-05 19:31:43
            [men_data_lida] => 0000-00-00 00:00:00
            [men_resposta] => 1
            [men_status] => 1
            [total_respostas] => 1
        )

)

I need to return a Count() from everyone with total_answer = 0. How can I do?

  • 1

    André stdClass is a class? What does it do?

  • 1

    It is return of the database... I need to count all who are with total_answers = 0;

1 answer

3


First you must filter the items of array to bring only what you need that in your specific case is total_respostas == 0 then use the count to count the quantity returned, example:

$total = count(array_filter($array, function($q){
    return $q->total_respostas == 0;
}));

References

Browser other questions tagged

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