PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
  • Home
  • Deploy
  • How-to
  • Tutorial
No Result
View All Result
PHP 101
No Result
View All Result
Home How-to

How to count array elements in PHP

Short guide on how to count array elements using PHP.

August 17, 2022
Reading Time: 2 mins read
PHP101.net - How to count array elements in PHP

PHP101.net - How to count array elements in PHP

Share on FacebookShare on Twitter

Quick solution

To quickly count the number of elements in a PHP array, we can simply use the count function:

// Source array
$fruits = ['Orange', 'Apple', 'Banana']

// Count the number of elements in $fruits array
echo count( $fruits ); // 3

To count array elements in PHP

The count function is the recommended function to count the number of elements in a PHP array or a Countable object. Beside the count function, sizeof is an alternative function which will deliver the same result.

Using count function

The full count function declaration as Official PHP Documentation:

count(Countable|array $value, int $mode = COUNT_NORMAL): int
  • $value is the array or Countable object to be counted.
  • $mode is the count mode, which can be adjusted to enable counting recursive array.
    Default value is COUNT_NORMAL.

Count recursive array

The $mode parameter by default, COUNT_NORMAL, can only count non-recursive array. To count all elements including the recursive (or nested) array elements, set the $mode parameter as COUNT_RECURSIVE:

count( $array, COUNT_RECURSIVE );

Example of counting recursive array:

// Source array
$food = ['fruits' => ['Orange', 'Apple', 'Banana'],
         'veggie' => ['Carrot', 'Collard', 'Pea']];

// Normal count mode
echo count( $food ); // 2

// Recursive count mode
echo count( $food, COUNT_RECURSIVE ); // 8

Using sizeof function

The sizeof function is an alias of count, and does the same functionality:

// Source array
$fruits = ['Orange', 'Apple', 'Banana']

// Count using sizeof function
echo sizeof( $fruits ); // 3

Final thoughts

It is easy and straightforward to count array elements in PHP, which can be used by either count or sizeof function. The count function is used more and is recommended as better human readable keyword.

You might also like

  • How to get current date with PHP
  • How to convert from string to array in PHP

References

  • PHP.net Official Documentation – count function
  • PHP.net Official Documentation – sizeof function
Tags: php countphp count array elementsphp count functionphp count recursive arrayphp sizeof
Previous Post

How to get user IP address using PHP

Next Post

How to copy files using PHP

Related Posts

PHP101.net - How to set timezone with PHP

How to set timezone with PHP easily (multiple methods)

March 9, 2023
PHP101.net - How to redirect to another page using PHP

How to redirect to another page using PHP

March 8, 2023

Table of Contents

    • Quick solution
    • To count array elements in PHP
    • Using count function
    • Count recursive array
    • Using sizeof function
    • Final thoughts
    • You might also like
    • References
Next Post
PHP101.Net - How to copy files using PHP

How to copy files using PHP

We deliver free and helpful resources for learning
and using PHP programming language.

About
Contact Us
Privacy Policy
Main Sections

Home
Deploy
How-to
Tutorial
You might be interested in...

How to redirect to another page using PHP

How to install Composer on Linux

How to install PHP GeoIP extension & GeoLite2 library (databases included)

Deploy a webserver with XAMPP on Windows for PHP development

How to set timezone with PHP easily (multiple methods)

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone.

No Result
View All Result
  • Home
  • Deploy
  • How-to
  • Tutorial

© 2022 PHP101.Net - Helpful PHP Tutorials, How-to, Tips and Tricks for everyone. | Privacy Policy

This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.