When I first started heavily using arrays in PHP, often times the number of array values or items would need to be known, and this would be accomplished by using a foreach statement and counting up the number of iterations. Looking back, this was the most inefficient and long way possible to grab the total array items. Instead, the proper way to count the number of values or items in an array, is to use the built-in count() function. Here is a quick example to retrieve the number of items in a PHP Array:
<?php $array = array("dog", "cat", "tiger", "zebra"); $count = count($array); echo $count; //will print 4 ?>
There you have it! Simply use the count() function to retrieve the total number of items in a PHP Array.