• PHP Video Tutorials

PHP array_count_values() Function



Definition and Usage

The array_count_values() function returns an associative array of values using the values of the input array as keys and their frequency in input array as values.

Syntax

array array_count_values ( array $input );

Parameters

Sr.No Parameter & Description
1

input (mandatory)

The input array of values to count

Return Values

It returns an associative array of values from input as keys and their count as value.

PHP Version

This function was first introduced in PHP Version 4.0.0.

Errors/Exceptions

This will throw E_WARNING for every element which is not string or integer.

Example

Try out following example −

<?php
   $input = array("orange", "mango", "banana", "orange", "banana" );
   print_r(array_count_values($input));
?>

This will produce the following result −

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

Example

Try out following example with all integer values −

<?php
   $input = array(10, 15, 30, 15, 10);
   print_r(array_count_values($input));
?>

This will produce the following result −

Array
(
    [10] => 2
    [15] => 2
    [30] => 1
)
php_function_reference.htm
Advertisements