Witam
Potrzebuję pomocy w zliczaniu class_id dla poszczególnych gałęzi.
Dane do drzewa wyglądają tak:
$data = array(
0 => array('id' => 1, 'parent_id' => 0, 'name' => 'products', 'class_id' =>1),
1 => array('id' => 2, 'parent_id' => 0, 'name' => 'services', 'class_id' =>1),
2 => array('id' => 3, 'parent_id' => 1, 'name' => 'home', 'class_id' =>2),
3 => array('id' => 4, 'parent_id' => 1, 'name' => 'car', 'class_id' =>1),
4 => array('id' => 5, 'parent_id' => 1, 'name' => 'garden', 'class_id' =>1),
5 => array('id' => 6, 'parent_id' => 4, 'name' => 'engine', 'class_id' =>1),
6 => array('id' => 7, 'parent_id' => 6, 'name' => 'oil', 'class_id' =>3),
7 => array('id' => 8, 'parent_id' => 3, 'name' => 'bedroom', 'class_id' =>1),
8 => array('id' => 9, 'parent_id' => 8, 'name' => 'bed', 'class_id' =>2),
9 => array('id' => 10, 'parent_id' => 5, 'name' => 'mower', 'class_id' =>3),
10 => array('id' => 11, 'parent_id' => 2, 'name' => 'shoping', 'class_id' =>5),
);
Moja funkcja tworząca drzewo:
function array_tree($array, $id = 'id', $parent_id = 'parent_id', $children = 'children') {
$tree = [[$children => []]];
$references = [&$tree[0]];
foreach ($array as $item) {
if (isset($references[$item[$id]])) {
$item[$children] = $references[$item[$id]][$children];
}
$references[$item[$parent_id]][$children][] = $item;
$references[$item[$id]] = & $references[$item[$parent_id]][$children][count($references[$item[$parent_id]][$children]) - 1];
}
return $tree[0][$children];
}
po dumpie drzewo układa się w ten sposób:
Array(2)
|
| [0] => Array(5)
| |
| | [id] => Integer(1) 1
| | [parent_id] => Integer(1) 0
| | [name] => String(8) "products"
| | [class_id] => Integer(1) 1
| | [children] => Array(3)
| | |
| | | [0] => Array(5)
| | | |
| | | | [id] => Integer(1) 3
| | | | [parent_id] => Integer(1) 1
| | | | [name] => String(4) "home"
| | | | [class_id] => Integer(1) 2
| | | | [children] => Array(1)
| | | | |
| | | | | [0] => Array(5)
| | | | | |
| | | | | | [id] => Integer(1) 8
| | | | | | [parent_id] => Integer(1) 3
| | | | | | [name] => String(7) "bedroom"
| | | | | | [class_id] => Integer(1) 1
| | | | | | [children] => Array(1)
| | | | | | |
| | | | | | | [0] => Array(4)
| | | | | | | |
| | | | | | | | [id] => Integer(1) 9
| | | | | | | | [parent_id] => Integer(1) 8
| | | | | | | | [name] => String(3) "bed"
| | | | | | | | [class_id] => Integer(1) 2
| | | | | | | |
| | | | | | |
| | | | | |
| | | | |
| | | |
| | | [1] => Array(5)
| | | |
| | | | [id] => Integer(1) 4
| | | | [parent_id] => Integer(1) 1
| | | | [name] => String(3) "car"
| | | | [class_id] => Integer(1) 1
| | | | [children] => Array(1)
| | | | |
| | | | | [0] => Array(5)
| | | | | |
| | | | | | [id] => Integer(1) 6
| | | | | | [parent_id] => Integer(1) 4
| | | | | | [name] => String(6) "engine"
| | | | | | [class_id] => Integer(1) 1
| | | | | | [children] => Array(1)
| | | | | | |
| | | | | | | [0] => Array(4)
| | | | | | | |
| | | | | | | | [id] => Integer(1) 7
| | | | | | | | [parent_id] => Integer(1) 6
| | | | | | | | [name] => String(3) "oil"
| | | | | | | | [class_id] => Integer(1) 3
| | | | | | | |
| | | | | | |
| | | | | |
| | | | |
| | | |
| | | [2] => Array(5)
| | | |
| | | | [id] => Integer(1) 5
| | | | [parent_id] => Integer(1) 1
| | | | [name] => String(6) "garden"
| | | | [class_id] => Integer(1) 1
| | | | [children] => Array(1)
| | | | |
| | | | | [0] => Array(4)
| | | | | |
| | | | | | [id] => Integer(2) 10
| | | | | | [parent_id] => Integer(1) 5
| | | | | | [name] => String(5) "mower"
| | | | | | [class_id] => Integer(1) 3
| | | | | |
| | | | |
| | | |
| | |
| |
| [1] => Array(5)
| |
| | [id] => Integer(1) 2
| | [parent_id] => Integer(1) 0
| | [name] => String(8) "services"
| | [class_id] => Integer(1) 1
| | [children] => Array(1)
| | |
| | | [0] => Array(4)
| | | |
| | | | [id] => Integer(2) 11
| | | | [parent_id] => Integer(1) 2
| | | | [name] => String(7) "shoping"
| | | | [class_id] => Integer(1) 5
| | | |
| | |
| |
|
NULL(0)
Chodzi mi o to aby dla każdej gałęzi zliczać liczbę unikalnych class_id, oraz wstawiac je pod nowym kluczem np:sum. Np: home powinno mieć sum = 2 (home = class_id(2),bedroom = class_id(1), bed = class_id(2), a główna products = home(1,2) + car(3) + garden(0) czyli 3 unikalne class_id. Chodzi o unikalne class_id a nie sumę...nie mogę sobię z tym poradzić
+