Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8019575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:25:36+00:00 2026-06-04T21:25:36+00:00

I’m trying to determine the count of each array within a multidimensional associative array.

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:25:38+00:00Added an answer on June 4, 2026 at 9:25 pm

    Basically, use a recursive function (countLevels in 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). $levels is 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 $data to be a member of the class, declare the variable with var $data and fill it inside the constructor.

    class Test {
        public function getCounts($data) {
            $counts = array();
            $this->countLevels($counts, 0, $data);
            return $counts;
        }
    
        // Counts the number of arrays in each level of $data
        private function countLevels(&$levels, $current, $parent) {
            if (! isset($levels[$current])) {
                $levels[$current] = 0;
            }
            foreach ($parent as $child) {
                $levels[$current]++;
                if (is_array($child)) {
                    $this->countLevels($levels, $current+1, $child);
                }
            }
            return $levels;
        }
    }
    
    // Let's use the class! Create an instance of it
    $test = new Test();
    
    // Fill the data array
    $data = [
        'food' => [
            'soup' => [
                'chicken_noodle' => '',
                'tomato' => '',
                'french onion' => '',
            ],
            'salad' => [
                'house' => '',
                'ceasar' => '',
            ],
        ],
        'drink' => [
            'soda' => [
                'coke' => '',
                'sprite' => '',
                'dr pepper' => '',
            ],
            'alcoholic' => [
                'whiskey' => [
                    'Jim Beam' => '',
                    'Jameson' => '',
                    'Bushmills' => '',
                ],
                'vodka' => [
                    'Stolichnaya' => '',
                    'Ketel One' => '',
                    'Grey Goose' => '',
                    'Belvedere' => '',
                ],
                'rum' => [
                    'Captain Morgan' => '',
                    'Bacardi' => '',
                ],
            ],
        ],
    ];
    
    // Count the data for arrays on each level
    $counts = $test->getCounts($data);
    
    // Print the results
    var_export($counts);
    

    Output:

    array (
      0 => 2,
      1 => 4,
      2 => 11,
      3 => 9,
    )
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Basically, what I'm trying to create is a page of div tags, each has
I would like to count the length of a string with PHP. The string
I am trying to render a haml file in a javascript response like so:
I am trying to loop through a bunch of documents I have to put
I have some data like this: 1 2 3 4 5 9 2 6
Thanks in advance for your help. I have a need within an application to
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.