PHP array_count_values() Function
Topic: PHP Array ReferencePrev|Next
Description
The array_count_values()
function counts how many times each unique value occurs in an array.
The following table summarizes the technical details of this function.
Return Value: | Returns an associative array containing all the unique values from the given array as keys and their number of occurrences or count as value. |
---|---|
Version: | PHP 4+ |
Syntax
The basic syntax of the array_count_values()
function is given with:
The following example shows the array_count_values()
function in action.
Example
Run this code »<?php
// Sample array
$letters = array("a", "b", "a", "c", "b", "a");
// Counting array values
print_r(array_count_values($letters));
?>
Parameters
The array_count_values()
function accepts the following parameters.
Parameter | Description |
---|---|
array | Required. Specifies the array of values to count. |
This function throws E_WARNING
for every element which is not string or integer.
More Examples
Here're some more examples showing how array_count_values()
function basically works:
The following example will count all the values in the numbers array:
Example
Run this code »<?php
// Sample array
$numbers = array(1, 2, 4, 5, 1, 2, 3, 1, 5, 2, 1);
// Counting array values
print_r(array_count_values($numbers));
?>