php

PHP Array_Count_Values() Function

If you want to return the occurrence of each value in an array, the array_count_values() function is used. It returns the total count of each element in the form of a key-value pair. The key represents the element and the value represents the occurrence of that value.

Syntax:
array_count_values(array_input)

It takes a single array as a parameter.

Returning Format:

Array
(
  [value] => total_count
  …..
  …..
)

Array holds the data in a linear fashion. It holds multiple elements at a time. Array() is used to create an array in PHP.

Syntax:
array(Value,……..)

Example 1:

In this example, we will create an array named “Flower” with 10 elements.

After that, we will use the array_count_values() to return the total occurrence of each element in an array.

<?php

//create an array named - Flower that has 10 values
$Flower=array('Flower_name', 'Flower_area', 'Flower_sepals', 'Flower_petals', 'flower_cost', 'Flower_name', 'Flower_area', 'Flower_sepals', 'Flower_petals','Flower_name');
 

//return total count of each count
print_r(array_count_values($Flower));
?>

Output:

We can see that:

  1. Flower_name occurred 3 times
  2. Flower_area occurred 2 times
  3. Flower_sepals occurred 2 rimes
  4. Flower_petals occurred 2 times
  5. flower_cost occurred 1 time

Example 2:

In this example, we will create an array named “Flower” with five key-value pairs. After that, we will use the array_count_values() to return the total occurrence of each element in an array.

<?php

//create an array named - Flower that has 5 elements
$Flower=array('0'=>'Flower_name', '1'=>'Flower_name', 2=>'Flower_area', 3=>'Flower_sepals', '4'=>'Flower_name');
 

//return total count of each count
print_r(array_count_values($Flower));
?>

Output:

We can see that:

  1. Flower_name value occurred 3 times
  2. Flower_area occurred 1 time
  3. Flower_sepals occurred 1 time

If the same key and value concurred more than one time, it counts only once.

Example 3:

In this example, we will create a key-value pairs array with five elements that have three same keys and value.

<?php

//create an array named - Flower that has 5 elements
$Flower=array('0'=>'Flower_name', '0'=>'Flower_name', 2=>'Flower_area', 3=>'Flower_sepals', '0'=>'Flower_name');
 

//return total count of each count
print_r(array_count_values($Flower));
?>

Output:

We can see that the “Flower_name” array occurred three times with 0 as the key. Since it is the same, it counts only one time. The remaining two values occurred only once. Hence, 1 is returned for them.

Conclusion

In this article, we learned how to get the count of each element in an array using the array_count_values() function. It returns the total count of each element in the form of a key-value pair. The key represents the element and the value represents the occurrence of that value.
We learned the two different array examples and returned the count of each element.

About the author

Gottumukkala Sravan Kumar

B tech-hon's in Information Technology; Known programming languages - Python, R , PHP MySQL; Published 500+ articles on computer science domain