Count values of elements of an array in PHP

3

I have the following array , for example:

Array
(
    [0] => Array
        (
            [ID] => 401675295
            [DATA] => 2016-06-28 15:33:07
            [TITULO] => Teste
            [TEXTO] => 
            [TIPO] => 4
            [NOME] => JOAO
        )

    [1] => Array
        (
            [ID] => 401675295
            [DATA] => 2016-06-28 16:34:31
            [TITULO] => Avaliação
            [TEXTO] => 
            [TIPO] => 3
            [NOME] => VITOR
        )

    [2] => Array
        (
            [ID] => 401675295
            [DATA] => 2016-06-28 16:34:41
            [TITULO] => Atendimento
            [TEXTO] => 
            [TIPO] => 3
            [NOME] => JOAO
        )

    [3] => Array
        (
            [ID] => 401675295
            [DATA] => 2016-06-28 16:34:52
            [TITULO] => Concluido
            [TEXTO] =>
            [TIPO] => 1
            [NOME] => PAULO
        )

)

How can I count [TYPE] by value? How do you know how many [TYPE] are equal to 3, or 4 for example?

    
asked by anonymous 29.06.2016 / 15:33

1 answer

4

It's quite simple. Use array_map to list all elements named TIPO . Then use the array_count_values function to know the result.

$tipos = array_map(function ($value) { return $value['TIPO'];}, $array);

array_count_values($tipos);

See an example at Ideone.com

    
29.06.2016 / 15:39