Categories
PHP

Count total number of items in nested arrays in PHP

Today I was working on some code that had arrays structured like this:

$foo = [
	'a' => [ 1, 2, 3, 4 ],
	'b' => [ 5, 6, 7 ],
];

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