0

Ok, so in my case i have an array kinda like this (but MUCH larger)

$array = [
    0 => "TOD",
    1 => "TOD",
    2 => "MAX",
    3 => "JEFF",
    4 => "TOD",
    5 => "MAX",
    6 => "JEFF",
    7 => "MAX",
    8 => "MAX"
];

Now my question is, is there a way to count how many occurences of TOD,JEFF & MAXthere are in the array $array and then store them into seperate variables, so for example desired outcome would be for there to be 3 seperate variables with these as there values (based off of the sample code shown above)

$todAmount = 3;
$jeffAmount = 2;
$maxAmount = 4;

I've done quite a bit of research and havn't really found out a good way to do this :/
Thanks for reading!

geegeeWap
  • 35
  • 9

2 Answers2

2

array_count_values() is probably what you need.

<?php

$array = [
    0 => "TOD",
    1 => "TOD",
    2 => "MAX",
    3 => "JEFF",
    4 => "TOD",
    5 => "MAX",
    6 => "JEF",
    7 => "MAX",
    8 => "MAX"
];
    print_r(array_count_values($array));
?>
Xatenev
  • 6,383
  • 3
  • 18
  • 42
0

That should help you...

http://php.net/manual/en/function.array-count-values.php

try

print_r(array_count_values($array));
tanaydin
  • 5,171
  • 28
  • 45