PHP Function Reference

PHP array_count_values() Function



The PHP array_count_values() function is used to count all the values of an array. It returns an associative array with values from the given array as keys and their frequency as values.

Syntax

array_count_values(array)

Parameters

array Required. Specify the array of values to count.

Return Value

Returns an associative array with values from the given array as keys and their frequency as values.

Exceptions

Throws E_WARNING for every element which is not string or int.

Example:

The example below shows the usage of array_count_values() function.

<?php
$Arr = array(10, 20, "hello", 20, "hello", "world");

//creating an array with count of values
$count_Arr = array_count_values($Arr);

print_r($Arr);
echo "\n";
print_r($count_Arr);
?>

The output of the above code will be:

Array
(
    [0] => 10
    [1] => 20
    [2] => hello
    [3] => 20
    [4] => hello
    [5] => world
)

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

❮ PHP Array Reference