Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

132
Visualizações
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);
}
over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

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

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda