DEV Community

Cover image for PHP Array Count
Istiaq Nirab
Istiaq Nirab

Posted on

PHP Array Count

By default PHP has two methods for count array.

  • array_count_values
  • count

array_count_values()

Using this function, the number of times a value in an array can be determined.

Example :

<?php
  $array = array(1, "hello", 1, "world", "hello");
  print_r(array_count_values($array));
?>
Enter fullscreen mode Exit fullscreen mode

Result :

Array (
    [1] => 2
    [hello] => 2
    [world] => 1
)
Enter fullscreen mode Exit fullscreen mode

count()

These function is very common & its return total amount of value from array .

<?php

 $array = array("a" => "green", "b" => "brown", "c" => 
  "blue", "red");
 echo count($array);

?>
Enter fullscreen mode Exit fullscreen mode
4
Enter fullscreen mode Exit fullscreen mode

Thanks

Top comments (0)