1

I have a array with thousand values.

$arr = array('Stone', 'Gem', 'Star', ..., 'Star', 'Rock', 'Salt', ..., 'Metal', 'Cotton', 'Gem',...);

my problem now is how can I count the values of the array.. for example:

Stone = 234
Gem = 231
Star = 123
Rock = 98
Salt = 265
...

any idea about this?

3
  • 1
    array_count_values() documentation link, returns the required array and the input array rests untouched Commented Sep 3, 2015 at 5:59
  • 1
    array_count_values($array); Commented Sep 3, 2015 at 5:59
  • 1
    possible duplicate of Count unique value from associative array Commented Sep 3, 2015 at 6:01

2 Answers 2

3

Like this:

$array_frequency = array_count_values($array);

The keys are the unique values of your input array, and their respective values is the number of occurrences of that value in the input array.

Sign up to request clarification or add additional context in comments.

Comments

2

Use array_count_values:

$occurrences = array_count_values($arr);

will result in something like:

Array
(
    [Stone] => 234
    [Gem] => 231
    [Star] => 123
)

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.