Count how often a certain value appears in an array

I know the count()php function , but which function counts how often the value appears in the array?

Example:

$array = array(
  [0] => 'Test',
  [1] => 'Tutorial',
  [2] => 'Video',
  [3] => 'Test',
  [4] => 'Test'
);

Now I want to count how often "Test" appears.

+5
source share
2 answers

For this, PHP has a function array_count_values.

Example:

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

Output:

Array
(
    [1] => 2
    [hello] => 2
    [world] => 1
)
+12
source

Try the function array_count_values, where you can find additional information about the function in the documentation: http://www.php.net/manual/en/function.array-count-values.php

Example from this page:

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

Will produce:

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

All Articles