tags:

views:

99

answers:

1

I'm running a loop basically that will make an array that contains a million numbers between 1 and 10, how do I iterate through it and count how many of each there are?

Like:

1 - 201491 times  
2 - 23091 times
+9  A: 

There's a native PHP function for that:

$count = array_count_values($array);
print_r($count);

will output:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
Sebastián Grignoli
Yep, PHP includes the kitchen sink.
Brenton Alker
I agree, get_kitchen_sink() might be in the PHP API also.But the one above really does work.
Sebastián Grignoli
Yep, good work, knew there was a function to do it in one go. Much thanks.
John