Mark Richards
Mark Richards

Reputation: 434

array group by in php or count array by group by

i have an array like :

Array
(
    [0] => Array
        (
            [0] => a
            [1] => a
            [2] => b
            [3] => c
            [4] => b
            [5] => c
            [6] => b
        )
 )

so i have to make array by group by with count total no. of array like (output) :

    a = 2
    b = 3
    c = 2

if array a is use two times than count of a will be 2.

Upvotes: 0

Views: 1955

Answers (2)

Manoj
Manoj

Reputation: 373

Copy paste code,

<?php
$rt=Array(Array('a','a','b','b','c','c'));
$out=array_count_values( $rt[0]);
print_r($out) ;
?> 

output

Array
(
 [a] => 2
 [b] => 2
 [c] => 2
)

Upvotes: 1

Vikram Jain
Vikram Jain

Reputation: 5588

Use this :

array_count_values(array)

Ex:

<?php
$a=array("Cat","Dog","Horse","Dog");
print_r(array_count_values($a));
?>

output:

Array ( [Cat] => 1 [Dog] => 2 [Horse] => 1 )

Upvotes: 5

Related Questions