English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Basic PHP Tutorial

Advanced PHP Tutorial

PHP & MySQL

PHP Reference Manual

Usage and Example of PHP array_count_values() Function

PHP Array Function Manual

Definition and Usage

array_count_values() The function is used to count the number of times all values appear in the array. It uses the values of the input array as keys, takes the number of times the value appears in the input array as the value, and returns an associative array of values.

Syntax

array array_count_values ( array $input );

  The key of the array is the value of the element in input; the value of the array is the number of times the value appears in input.

Parameter

Serial NumberParameters and Description
1

input (required)

Count the values of this array

Return Value

Returns an associative array using the values of the input array as keys, and the number of times the value appears in the array as the value.

PHP Version

This function was originally introduced in PHP 4Introduced in version .0.0.

Error/Exception

Throw a warning error (E_WARNING) for each element in the array that is not a string or integer type.

Online Example

Test the following array_count_values() example-

<?php
   $input = array("orange", "mango", "banana", "orange", "banana");
   print_r(array_count_values($input));
?>
Test and See‹/›

Output Result:

Array
(
    [orange] => 2
    [mango] => 1
    [banana] => 2
)

Online Example

Try the following example using all integer values-

<?php
   $input = array(10, 15, 30, 15, 10);
   print_r(array_count_values($input));
?>
Test and See‹/›

Output Result:

Array
(
    [10] => 2
    [15] => 2
    [30] => 1
)

PHP Array Function Manual