Count number of items in multi dimensional array

I have a multi dimensional array that I want to do some processing on before I display the data. So my array looks like this

Array ( 
[0] => Array ( 
	[0] => Array ( 
				[0] => I88 [1] => husband [2] => Herbert Charles Bramble [3] => M ) 
				[1] => Array ( [0] => I89 [1] => wife [2] => Annie Eva Fanny Binstead [3] => F ) 
				[2] => Array ( [0] => I4 [1] => child [2] => Audrey Frances Beryl Bramble [3] => F ) 
				[3] => Array ( [0] => I1364 [1] => child [2] => Barry Leo Newton [3] => M ) ) 
	[1] => Array ( 
				[0] => Array ( [0] => I83 [1] => husband [2] => Herbert William Hilton [3] => M ) 
				[1] => Array ( [0] => I89 [1] => wife [2] => Annie Eva Fanny Binstead [3] => F ) 
				[2] => Array ( [0] => I162 [1] => child [2] => Muriel Evelyn Mavis Hylton [3] => F ) ) 
	[2] => Array ( 
				[0] => Array ( [0] => I159 [1] => husband [2] => Alfred Darrington [3] => M ) 
				[1] => Array ( [0] => I89  [1] => wife [2] => Annie Eva Fanny Binstead [3] => F ) 
				[2] => Array ( [0] => I161 [1] => child [2] => Doris Ivy Ellen (Joan) Darrington [3] => F ) 
				[3] => Array ( [0] => I160 [1] => child [2] => Patricia Gwendoline Fanny Darrington [3] => F ) ) ) 

What I need to do is to get a count of the number of ‘child’ in each of the array groups.

I have tried lots of variants around

$num_children = count(array_keys($my_array_variable,'child'));

without success.

Can anyone give me some suggestions please?

PHP 7.4 now with syntactic sugar

echo array_reduce($arr, fn($count, $itm) => $count += count($itm));

Thanks for that @chorn but I cannot guarantee that users will have php7 even though they should so I do need something that will work with >5.6.

then use a closure instead

https://www.php.net/manual/en/class.closure.php

Sponsor our Newsletter | Privacy Policy | Terms of Service