Home Tutorial Php Phparray PHP array count

 
 

PHP array count
Posted on: October 29, 2009 at 12:00 AM
This section demonstrates how to count the number of elements in array using array_count_values() function

  • The PHP array_count_values() function is used to count the elements in the array.
  • It returns the number of counting with values in associative array form.
  • In returned array, key is the array's values and values are the number of occurrences.


Example of PHP Array Count

<?php
    $ar
=array(1=>"aaa",2=>"bbb",3=>"ccc",4=>"ddd",5=>"aaa",6=>"bbb",7=>"ggg");
    $arr
=array_count_values($ar);
    foreach
($arr as $key=>$val)
      print_r($arr);
    echo
"<br>key $key value $val";
?>

    Output

    Array ( [aaa] => 2 [bbb] => 2 [ccc] => 1 [ddd] => 1 [ggg] => 1 )
    key aaa value 2
    key bbb value 2
    key ccc value 1
    key ddd value 1
    key ggg value 1

 

Related Tags for PHP array count :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.