0

I'm having problems return a correct number of items in a multidimensional array and i have no idea what I'm doing wrong so I'm kinda confused.

// Build test array

$marks = Array();

$marks[] = array("id" => 1, "parent_id" => "0", "title" => "Games");
$marks[] = array("id" => 2, "parent_id" => "0", "title" => "Food");
$marks[] = array("id" => 3, "parent_id" => "0", "title" => "Houses");
$marks[] = array("id" => 4, "parent_id" => "0", "title" => "Cities");

$marks[2]['child'][] = array("id" => 4, "parent_id" => "0", "title" => "Big House");
$marks[2]['child'][] = array("id" => 4, "parent_id" => "0", "title" => "Small House");
$marks[2]['child'][] = array("id" => 4, "parent_id" => "0", "title" => "Castle");

$marks[2]['child'][1]['child'][] = array("id" => 4, "parent_id" => "0", "title" => "Living Room");
$marks[2]['child'][1]['child'][] = array("id" => 4, "parent_id" => "0", "title" => "Kitchen");

    function count_recursive ($array, $limit) { 
        $count = 0; 
        foreach ($array as $id => $_array) { 
            if (is_array ($_array) && $limit > 0) { 
                $count += count_recursive ($_array, $limit - 1); 
            } else { 
                $count += 1; 
            } 
        } 
        return $count; 
    } 

echo '###' . count_recursive($marks, 5);

echo '<pre>' . print_r($marks, 1) . '</pre>';

The odd thing is that it's returning 27?? I don't understand where it gets that value when it's supposed to be 9.

I also tested

echo count($marks, COUNT_RECURSIVE);

But it returns 38 so I'm really confused!

Help would be much appreciated.

1 Answer 1

1

You're counting the fields in the arrays. You seem to be asking how to count whole arrays that each have an id and so on -- that's what would yield 9. To do that, instead on incrementing the count when is_array is false, increment when it's true. Instead of passing that element back in recursively, see if it has a ['child'] and pass that recursively. Otherwise the 'child' collection itself gets counted and you get 11. Or, you could increment the counter only if 'id' is set.

Here's how COUNT_RECURSIVE gets 38:

$marks has four values, indexed as 0, 1, 2, and 3. That makes a count of 4.

$marks[0] through $marks[3] each have three named elements: 'id', 'parent_id', and 'title', together making 12. The count is now 16.

$marks[2] has a 'child' also. That makes 17.

$marks[2]['child'] has three elements indexed as 0, 1, and 2. That makes 20.

Each of those has three named elements, making 29.

$marks[2]['child'][1] has a 'child', making 30.

That has elements 0 and 1, making 32.

Each of those two has three named elements. 38.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.