PHP array_count_values() Function

PHP array_count_values() function is “used to count the occurrences of each unique value in an array.”

Syntax

array array_count_values($array )

Parameters

$array(required): This parameter is the array for which we need to calculate the count of values present in it.

Return value

It returns an associative array with key-value pairs in which keys are the original values from the input array, and values are the count of each value.

Visual RepresentationVisual Representation of PHP array_count_values() Function

Example: How to Use array_count_values() function

<?php

$arr = ["Database", "Firebase", "Pinecon", "MongoDB", "Database", "MongoDB"];

$result = array_count_values($arr);

print_r($result);

Output

Array
(
 [Database] => 2
 [Firebase] => 1
 [Pinecon] => 1
 [MongoDB] => 2
)

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.