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.