2

how can echo count of each value in php array? for example in this array:

 $array = array(test,test,ok,test,ok); 

now how can echo count test or ok in this array?

saber
  • 336
  • 5
  • 15

2 Answers2

8

Example straight from the official PHP.net

<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)

If you want to echo then throw that command in.

For your example:

<?php
    $array = array(test,test,ok,test,ok); 
    print_r(array_count_values($array));
?>

The output is:

Array
(
    [test] => 3
    [ok] => 2
)

To echo try a foreach loop or something like this:

echo "Test = ".array_count_values($array)['test'];

The output is:

Test = 3
les
  • 564
  • 7
  • 19
1

simple way:

print_r(array_count_values($array));

if you want just the "ok":

echo array_count_values($array)['ok'] // output 2
roy
  • 585
  • 5
  • 14