• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Get Array Length with php count() Function

Get Array Length with php count() Function

March 24, 2022 Leave a Comment

In php, we can get the length of an array easiest with the php count() function.

$array = array(1, 2, 3, 4, 5);

echo count($array);

// Output:
5

When working with arrays in php, it can be valuable to be able to easily gather information of the properties of an array.

One such situation is when you want to get the size of an array.

To get the length of an array, you can use the php count() function.

Just pass an array to count() to get the count of the number of elements in the array.

Below is a simple example in php of how to use the count() function to get the length of an array.

$array = array(1, 2, 3, 4, 5);

echo count($array);

// Output:
5

Find the Size of an Array with sizeof() Function in php

Another function which is useful in php is the sizeof() function. The sizeof() function is an alias of count().

You can use the sizeof() function to get the size of an array, but it is better to use count() to find the length of an array.

Below is an example in php showing that sizeof() returns the same count as count().

$array = array(1, 2, 3, 4, 5);

echo sizeof($array);

// Output:
5

Using count() to Count the Number of Elements in a Multi-Dimensional Array in php

With the php count() function, we can do more than just find the length of a one-dimensional array.

We can also get the size of a multi-dimensional array by passing ‘COUNT_RECURSIVE’ to the optional ‘mode’ parameter.

The ‘COUNT_RECURSIVE’ option counts the number of elements recursively and will go into any sub arrays to get the length and size of those arrays.

Below is an example in php of how you can use ‘COUNT_RECURSIVE’ to get the number of elements of a multi-dimensional array.

$array = array(1, 2, array(3, 4, 5), 6, 7);

var_dump(count($array));
var_dump(count($array, COUNT_RECURSIVE));

//Output:
int(5)
int(8)

As shown above, when we don’t pass ‘COUNT_RECURSIVE’, we get the length of the array’s first dimension. But after passing ‘COUNT_RECURSIVE’, we get the total number of elements.

Hopefully this article has been useful for you to learn how to use the php count() function to get an array’s length in php.

Other Articles You'll Also Like:

  • 1.  php array_shift() – Remove the First Element from Array
  • 2.  How to Check If String Contains Substring in PHP
  • 3.  Reversing an Array in php with array_reverse() Function
  • 4.  Get Last Day of Month in php
  • 5.  How to Parse URLs with php parse_url() Function
  • 6.  php Convert Radians to Degrees with php rad2deg() Function
  • 7.  php var_dump() Function – Print Information about Variable Type and Value
  • 8.  How to Add Items to Array in php
  • 9.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 10.  php Random Number Generator with rand Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy