$tree = array(
1 => array(
'id' => 1,
'parent' => 0,
'name' => 'Каталог 1',
'count' => 0,
'childs' => array(
4 => array(
'id' => 4,
'parent' => 1,
'name' => 'Каталог 4',
'count' => 2,
'childs' => array(
5 => array(
'id' => 5,
'parent' => 4,
'name' => 'Каталог 5',
'count' => 6
)
)
)
)
),
2 => array(
'id' => 2,
'parent' => 0,
'name' => 'Каталог 2',
'count' => 3
),
3 => array(
'id' => 3,
'parent' => 0,
'name' => 'Каталог 3',
'count' => 5,
'childs' => array(
6 => array(
'id' => 6,
'parent' => 3,
'name' => 'Каталог 6',
'count' => 8
),
7 => array(
'id' => 7,
'parent' => 3,
'name' => 'Каталог 7',
'count' => 1
)
)
),
10 => array(
'id' => 10,
'parent' => 0,
'name' => 'Каталог 10',
'count' => 0
)
);
$f = function ($f, &$data) {
$count = 0;
foreach ($data as $key => &$value) {
if (!isset($value['count'])) {
$value['count'] = 0;
}
if (isset($value['childs'])) {
$value['count'] += $f($f, $value['childs']);
}
$count += $value['count'];
}
return $count;
};
$f($f, $tree);
header('content-type: text/html; charset=utf-8');
echo '<pre>';
@print_r($tree);
echo '</pre>';
exit(__FILE__ . ': ' . __LINE__);