0

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.

2 Answers 2

5

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 )
Sign up to request clarification or add additional context in comments.

1 Comment

+1 - nice and simple if one knows there is a function in PHP for this.
1

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
)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.