Drzewko PHP - zliczanie id produktu dla gałęzi

0

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ć
+

0

Dodałem funkcje to poruszania się po drzewie

 

 function finds($array, &$tmp = null) {
            foreach ($array as $value) {
                if (!empty($value['children'])) {
                    if (count($value['children']) > 1) {
                        foreach ($value['children'] as $value) {
                            finds($value['children'], $tmp);
                        }
                    } else {
                        return finds($value['children'], $tmp);
                    }
                } else {
                    array_push($tmp, $value);
                    unset($value);
                }
            }
        }

function up(&$arr, $id, &$map, $class = null) {
            foreach ($arr as $k => $x) {
               if ($x['id'] == $id) {
                    $pid = $x['parent_id'];
                    if (!is_null($class)) {
                        $map[$x['id']]['class'][] = $class;
                        $map[$x['id']]['suma'] = count(array_count_values($map[$x['id']]['class']));
                    }
                    $map[$x['id']]['class'][] = $x['class_id'];
                    $map[$x['id']]['suma'] = count(array_count_values($map[$x['id']]['class']));
                    return up($arr, $pid, $map, $x['class_id']);
                }
            }
        }

        $tree = array_tree($data);
        $tmp = array();
        finds($tree, $tmp);
        $mapa = array();

        foreach ($tmp as $k => $v) {
            $id = $v['parent_id'];
            $cid = $v['class_id'];

            up($data, $id, $mapa, $cid);
        }

        var_dump($mapa);

Teraz dostaje takie wyniki:

 
 [8] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  2
|        |        |        [1] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        [3] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  1
|        |        |        [1] => Integer(1)  2
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        [1] => Array(2)
|        |        
|        |        [class] => Array(6)
|        |        |        
|        |        |        [0] => Integer(1)  2
|        |        |        [1] => Integer(1)  1
|        |        |        [2] => Integer(1)  1
|        |        |        [3] => Integer(1)  1
|        |        |        [4] => Integer(1)  1
|        |        |        [5] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        [6] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  3
|        |        |        [1] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        [4] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  1
|        |        |        [1] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  1
|        |        

|        [5] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  3
|        |        |        [1] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        [2] => Array(2)
|        |        
|        |        [class] => Array(2)
|        |        |        
|        |        |        [0] => Integer(1)  5
|        |        |        [1] => Integer(1)  1
|        |        |        

|        |        [suma] => Integer(1)  2
|        |        

|        
0

Tylko wyniki są zliczane nie dla wszystkich liści, np: dla products zły wynik :-(

1 użytkowników online, w tym zalogowanych: 0, gości: 1