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

1 Answer 1

13

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

3 Comments

Yep, PHP includes the kitchen sink.
I agree, get_kitchen_sink() might be in the PHP API also. But the one above really does work.
Yep, good work, knew there was a function to do it in one go. Much thanks.

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.