Skip to content
Advertisement

Node Tree break down as Array

is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don’t know how to make it more efficient. is that possible to generate the array base one this node tree? That is the code that I have done. But I have no idea how to create 1,3,2 and 1,4 as an array. And I don’t know how to make it more efficient.

That is the array:

 array (
  'pole number 1' => 
  array (
    0 => 'pole number 1',
    1 => 'TRUE',
    'children' => 
    array (
      'pole number 3' => 
      array (
        0 => 'pole number 3',
        1 => 'FALSE',
        2 => 'pole number 1',
        'children' => 
        array (
          'pole number 2' => 
          array (
            0 => 'pole number 2',
            1 => 'FALSE',
            2 => 'pole number 3',
          ),
        ),
      ),
      'pole number 4' => 
      array (
        0 => 'pole number 4',
        1 => 'FALSE',
        2 => 'pole number 1',
      ),
    ),
  ),
  'pole number 5' => 
  array (
    0 => 'pole number 5',
    1 => 'TRUE',
    'children' => 
    array (
      'pole number 6' => 
      array (
        0 => 'pole number 6',
        1 => 'FALSE',
        2 => 'pole number 5',
        'children' => 
        array (
          'pole number 7' => 
          array (
            0 => 'pole number 7',
            1 => 'FALSE',
            2 => 'pole number 6',
          ),
        ),
      ),
      'pole number 8' => 
      array (
        0 => 'pole number 8',
        1 => 'FALSE',
        2 => 'pole number 5',
      ),
    ),
  ),
)
1
--3
---2
--4

Expected Output

[
 [1,3,2],
 [1,4]
]
foreach ($tree as $key => $value) {
    $result = traverseTree($value);
}

function traverseTree($tree = array())
{
    if ($tree[1] && !empty($tree['children'])) {
        // if no parent id than that is root
        $result[$tree[0]] = $tree[0];
        traverseTree($tree['children']);
    } else {
        foreach ($tree as $key => $node) {
            var_dump($node[1]);
            if ($node[1]) {
                $result[$node[2]][] = $node[0];
            }
        }
    }

    var_dump($result);
}

Advertisement

Answer

Your recursion strategy seems to be ok but you need to push another recursion call to stack by passing the children array if it is set. Next is to loop over each individual result subarray returned from the recursion call and current key name to it as shown below.

<?php

function collectNodes($tree){
    $result = [];
    foreach($tree as $name => $details){
        if(isset($details['children'])){
            $sub_result = collectNodes($details['children']);
            foreach($sub_result as $sr){
                $sr = array_merge([$name],$sr);
                $result[] = $sr;
            }
        }else{
            $result[] = [$name];
        }
    }    
    return $result;
}

print_r(collectNodes($tree));

Demo: http://sandbox.onlinephpfunctions.com/code/bb826c6ce98976ce2b4c694946d79e765f4b6330

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement