# Count total number of items in nested array in PHP

Today, We want to share with you Count total number of items in nested array in PHP.In this post we will show you PHP count items in a multi-dimensional array, hear for Multi-Dimensional array count in PHP we will give you demo and example for implement.In this post, we will learn about Count total number of items in nested arrays in PHP with an example.

## Count total number of items in nested array in PHP

There are the Following The simple About total number of items in nested array Full Information With Example and source code.

As I will cover this Post with live Working example to develop php count array elements with specific value, so the count multidimensional array php for this example is following below.

### PHP Multidimensional Nested Array count

Today We were working on Simple PHP code that had best way to understand arrays structured like this:

[php]
\$item_no = [
‘a’ => [ 1, 2, 3, 4 ],
‘b’ => [ 5, 6, 7 ],
];
[/php]

We needed to simple calculate the total number of items in the nested php arrays. I didn’t ANY find a PHP function to help me with that therefor We came up with this idea:

[php]
\$total_count = array_sum( array_map( ‘count’, \$item_no ) ); // This returns 7.
[/php]

### How this works

The array_map( ‘count’, \$item_no ) part replaces the subarrays in \$item_no with the result of calling count()on them:

READ :  vue js serialize form submit in Laravel

[php]
\$subarray_counts = array_map( ‘count’, \$item_no );
var_dump( \$subarray_counts );

/*
* Output:
*
* array(2) {
* ‘a’ =>
* int(4)
* ‘b’ =>
* int(3)
* }
*/
[/php]

The array_sum() PHP function then calculates the Addition of these item counts.

Please let me know if you found this PHP Array idea useful! 🙂