array_count_values() function in PHP


The array_count_values() function returns an array with the number of occurrences for each value. It returns an associative array. The returned array has keys as the array’s values, whereas values as the count of the passed values.

Syntax

array_count_values(arr)

Parameters

  • arr − The array for which we want to count the values.

Return

The array_count_values() function returns an associative array. The returned array has keys as the array’s values, whereas values as the count of the passed values.

Example

Live Demo

<?php
   $arr = array("Laptop","Keyboard","Mouse","Keyboard","Keyboard", "Mouse","Keyboard");
   print_r(array_count_values($arr));
?>

Output

Array
(
   [Laptop] => 1
   [Keyboard] => 4
   [Mouse] => 2
)

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements