I’m trying to determine the count of each array within a multidimensional associative array. For example, if I have an array like:
array(
'food' => array(
'soup' => array(
'chicken noodle' =>
'tomato' =>
'french onion' =>
),
'salad' => array(
'house' =>
'ceasar' =>
),
),
'drink' => array(
'soda' => array(
'coke' =>
'sprite' =>
'dr pepper' =>
),
'alcoholic' => array(
'whiskey' => array(
'Jim Beam' =>
'Jameson' =>
'Bushmills' =>
),
'vodka' => array(
'Stolichnaya' =>
'Ketel One' =>
'Grey Goose' =>
'Belvedere' =>
),
'rum' => array(
'Captain Morgan' =>
'Bacardi' =>
),
),
),
)
Not too sure how to explain this so I’ll do my best. I want to find out the count of each array inside the array. So the values I’d expect from this array would look something like:
array(
// total count of all arrays at "level 1" (i.e. food & drink)
0 => 2,
// total count of all arrays at "level 2" (i.e. soup, salad, soda, alcoholic)
1 => 4,
// total count of all arrays at "level 3" (i.e. chicken noodle, tomato, french onion, house, ceasar, coke, sprite, dr pepper, whiskey, vodka, rum)
2 => 11,
// total count of all arrays at "level 4" (i.e. Jim Beam, Jameson, Bushmills, Stolichnaya, Ketel One, Grey Goose, Belvedere, Captain Morgan, Bacardi)
3 => 9
)
Now I’m aware that I’m only using the index keys for the values as opposed to the values, and while it would be easier to get these values if the array was a standard indexed array (I think I’m using correct terminology here, feel free to correct me- the whole point of course is for me to understand here- not for someone to just "give me an answer" :)) since I could then just loop through the array in a for($i = 0 $i < count($array); $i++) loop and increment my counts accordingly. I’ve searched SO for such an issue but haven’t found it, if I missed it though feel free to simply point me to it. Thanks!
Basically, use a recursive function (
countLevelsin the example below) that goes through every level of the array, counting each index that is an array, and adds that count to a master "counting" array ($levels).$levelsis passed by reference, and the count of each level is updated directly on it. You can stick this right into your class, as I did below.Below is what I think you want your class to look like. If you want
$datato be a member of the class, declare the variable withvar $dataand fill it inside the constructor.Output: