CopyPastehas never been so tasty!

Count values in array using array_count_values in PHP

by anonymous

  • 0
  • 0
  • 0
88 views

http://codingvilla.com/count-values-array-php-single-article-681.aspx

Purpose:

array_count_values function counts the number of value occurrence in array. It takes one parameter that is array. array_count_values(array).


Code:


1. <?php
2. $students=array("Rizwan","Tahreem","Fatima","Malik",
"Rizwan","Tahreem","Malik");
3. print_r(array_count_values($students));
4. ?>

Output:

Array ( [Rizwan] => 2 [Tahreem] => 2 [Fatima] => 1 [Malik] => 2 )

Explanation:

In the code above on line 2 we have declared and initialize an array. Then on line 3 we have passed it to array_count_values function to count number of occurrence of the value. Then we have passed it to print_r function to show output of the array.

Add A Comment: