This guide will show you how to the count the amount of items in array with PHP. Doing this is simple as calling a native function. There are many reasons for counting the amount of items you have in your array. As I build data sets, I will often time use the count function to see how many pieces of data I added to my array at the end of a loop.
To start I will keep it simple and create and array with the name $array
and add to it some random values as seen below.
#array
$array = array(1, 2, 3, 4, 4, 6, "string", "digi dank");
Next we will simply call the php native function count()
on our array variable $array
and it will tell us the amount if items that are in it.
#count array items
echo count($array);
Here I simply, echo
the count for demo purposes. As you can see below, the results for my example array is:
8
As you can see it was easy to count the items in an array using PHP. The full code might look something like below when we put it all together. There are many different use cases for counting the items in an array. If you are having troubles or want to share what projects you work on that requires counting arrays, please drop a comment below!
#array
$array = array(1, 2, 3, 4, 4, 6, "string", "digi dank");
#count array items
echo count($array);
If you would like to suggest a new article or video to be released, please reach out via the Contact Page. I look forward to hearing your feedback and providing content that is helpful for you!