0

i have an array thats it a result from the db query, and i want to make a tree structure based on it.

I am using php, maybe its need a recursive function, but i dont have any ideia how to do it.

here is my array of data:

Array
(
    [0] => Array
        (
            [nome] => adicionar
            [sub_funcao] => sub_funcao
            [nome_funcao] =>
            [tipo_sub_funcao] => 3
            [check_dropdown] =>
            [posicao] => 1
            [dropdown_id] => 1
            [dropdown_where] =>
            [pagina] => ddc_funcoes
        )

    [1] => Array
        (
            [nome] => drop teste 1
            [sub_funcao] => sub_funcao
            [nome_funcao] => teste
            [tipo_sub_funcao] => 3
            [check_dropdown] =>
            [posicao] => 5
            [dropdown_id] => 1.1
            [dropdown_where] => 1
            [pagina] => ddc_funcoes
        )

    [2] => Array
        (
            [nome] => deletar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_deletar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 3
            [dropdown_id] => 3
            [dropdown_where] =>
            [pagina] => ddc_funcoes
        )

    [3] => Array
        (
            [nome] => editar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_editar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 2
            [dropdown_id] => 2
            [dropdown_where] =>
            [pagina] => ddc_funcoes
        )

    [4] => Array
        (
            [nome] => sub funcao teste 1
            [sub_funcao] => sub_funcao
            [nome_funcao] => teste
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 4
            [dropdown_id] =>
            [dropdown_where] => 1
            [pagina] => ddc_funcoes
        )

    [5] => Array
        (
            [nome] => sub funcao teste 2
            [sub_funcao] => sub_funcao
            [nome_funcao] => teste
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 1
            [dropdown_id] =>
            [dropdown_where] => 1.1
            [pagina] => ddc_funcoes
        )

    [6] => Array
        (
            [nome] => sub funcao
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_adicionar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] => adicionar
            [posicao] => 1
            [dropdown_id] =>
            [dropdown_where] => 1
            [pagina] => ddc_funcoes
        )

    [7] => Array
        (
            [nome] => sub funcao dropdown
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_adicionar[sub_funcao_dropdown]
            [tipo_sub_funcao] => 1
            [check_dropdown] => adicionar
            [posicao] => 3
            [dropdown_id] =>
            [dropdown_where] => 1
            [pagina] => ddc_funcoes
        )

    [8] => Array
        (
            [nome] => sub funcao tab
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_adicionar[sub_funcao_tab]
            [tipo_sub_funcao] => 1
            [check_dropdown] => adicionar
            [posicao] => 2
            [dropdown_id] =>
            [dropdown_where] => 1
            [pagina] => ddc_funcoes
        )
)

some database structure

what i am trying to achieve here is dropdown_id is the head of the level and dropdown_where is where it should go.

for example, dropdown_id = 1 will receive dropdown_where = 2, and if its level 2, dropdown_id = 1.1 will receive dropdown_where = 1.1.

Dont have any ideia if is right to do this way, so i will be glad with some advices

Expected output:

Array
(
    [0] => Array
        (
            [nome] => adicionar
            [sub_funcao] => sub_funcao
            [nome_funcao] =>
            [tipo_sub_funcao] => 3
            [check_dropdown] =>
            [posicao] => 1
            [dropdown_id] => 1
            [dropdown_where] =>
            [pagina] => ddc_funcoes
            [sub_funcao_arr] => Array
                (
                    [0] => Array
                        (
                            [nome] => sub funcao
                            [sub_funcao] => sub_funcao
                            [nome_funcao] => funcoes_adicionar[sub_funcao]
                            [tipo_sub_funcao] => 1
                            [check_dropdown] => adicionar
                            [posicao] => 1
                            [dropdown_id] =>
                            [dropdown_where] => 1
                            [pagina] => ddc_funcoes
                        )

                        [1] => Array
                            (
                                [nome] => sub funcao dropdown
                                [sub_funcao] => sub_funcao
                                [nome_funcao] => funcoes_adicionar[sub_funcao_dropdown]
                                [tipo_sub_funcao] => 1
                                [check_dropdown] => adicionar
                                [posicao] => 3
                                [dropdown_id] =>
                                [dropdown_where] => 1
                                [pagina] => ddc_funcoes
                            )

                        [2] => Array
                            (
                                [nome] => sub funcao tab
                                [sub_funcao] => sub_funcao
                                [nome_funcao] => funcoes_adicionar[sub_funcao_tab]
                                [tipo_sub_funcao] => 1
                                [check_dropdown] => adicionar
                                [posicao] => 2
                                [dropdown_id] =>
                                [dropdown_where] => 1
                                [pagina] => ddc_funcoes
                            )

                        [3] => Array
                            (
                                [nome] => sub funcao teste 1
                                [sub_funcao] => sub_funcao
                                [nome_funcao] => teste
                                [tipo_sub_funcao] => 1
                                [check_dropdown] =>
                                [posicao] => 4
                                [dropdown_id] =>
                                [dropdown_where] => 1
                                [pagina] => ddc_funcoes
                            )

                        [4] => Array
                            (
                                [nome] => drop teste 1
                                [sub_funcao] => sub_funcao
                                [nome_funcao] => teste
                                [tipo_sub_funcao] => 3
                                [check_dropdown] =>
                                [posicao] => 5
                                [dropdown_id] => 1.1
                                [dropdown_where] => 1
                                [pagina] => ddc_funcoes
                                [dropdown] => Array
                                    (
                                        [0] => Array
                                            (
                                                [nome] => sub funcao teste 2
                                                [sub_funcao] => sub_funcao
                                                [nome_funcao] => teste
                                                [tipo_sub_funcao] => 1
                                                [check_dropdown] =>
                                                [posicao] => 1
                                                [dropdown_id] =>
                                                [dropdown_where] => 1.1
                                                [pagina] => ddc_funcoes
                                            )
                                    )
                            )
                    )

        )

    [1] => Array
        (
            [nome] => editar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_editar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 2
            [dropdown_id] => 2
            [dropdown_where] =>
            [pagina] => ddc_funcoes
        )

    [2] => Array
        (
            [nome] => deletar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_deletar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] =>
            [posicao] => 3
            [dropdown_id] => 3
            [dropdown_where] =>
            [pagina] => ddc_funcoes
        )
)

1 Answers1

0

I spent a couple hours thinking about this one today. The main part of why this was so difficult to think about is because your array does not include the dropdown_id as the key for each record. If you do this, it makes this process much easier.

(I know in your comment you said that the key for each record is not the record dropdown, but I highly recommend you do)

I took the answer from here and adapted it. This solution also has the added benefit of only needing a single dropdown_id column in the database, as the parent_id can be figured out.

Dropdown IDs look like this:

1
  1.1
2
3
  3.1
    3.1.1
    3.1.2
  3.2
4
  4.1
  4.2

So to make this solution work you must do 2 things:

  1. The $flat array must include the dropdown_id as the key for each row
  2. You need to give each row a dropdown_id
$flat = [
    1 => 
    [
      'nome' => 'sub funcao teste 1',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => '',
      'tipo_sub_funcao' => '3',
      'check_dropdown' => '',
      'posicao' => '1',
      'dropdown_id' => '1',
      'pagina' => 'ddc_funceos',
    ],
    '1.1' => 
    [
      'nome' => 'sub teste 1',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'teste',
      'tipo_sub_funcao' => '3',
      'check_dropdown' => '',
      'posicao' => '5',
      'dropdown_id' => '1.1',
      'pagina' => 'ddc_funceos',
    ],
    3 => 
    [
      'nome' => 'deletar',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'funcoes_deletar[sub_funcao]',
      'tipo_sub_funcao' => '1',
      'check_dropdown' => '',
      'posicao' => '3',
      'dropdown_id' => '3',
      'pagina' => 'ddc_funceos',
    ],
    2 => 
    [
      'nome' => 'editar',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'funcoes_editar[sub_funcao]',
      'tipo_sub_funcao' => '3',
      'check_dropdown' => '',
      'posicao' => '2',
      'dropdown_id' => '2',
      'pagina' => 'ddc_funceos',
    ],
    '1.1.1' => 
    [
      'nome' => 'sub funcao teste 2',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'teste',
      'tipo_sub_funcao' => '1',
      'check_dropdown' => '',
      'posicao' => '1',
      'dropdown_id' => '1.1.1',
      'pagina' => 'ddc_funceos',
    ],
    '1.2' => 
    [
      'nome' => 'sub funcao dropdown',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'funcoes_adicionar[sub_funcao]',
      'tipo_sub_funcao' => '1',
      'check_dropdown' => 'adicionar',
      'posicao' => '3',
      'dropdown_id' => '1.2',
      'pagina' => 'ddc_funceos',
    ],
    '1.3' => 
    [
      'nome' => 'sub funcao tab',
      'sub_funcao' => 'sub_funcao',
      'nome_funcao' => 'funcoes_adicionar[sub_funcao_tab]',
      'tipo_sub_funcao' => '1',
      'check_dropdown' => 'adicionar',
      'posicao' => '2',
      'dropdown_id' => '1.3',
      'pagina' => 'ddc_funceos',
    ],
];

function determineParentId($id)
{
    $_id = explode('.', $id);
    if (count($_id) > 1) {
        array_pop($_id);
        if (count($_id) > 1) {
            return implode('.', $_id);
        } else {
            return $_id[0];  
        }
    } 

    return 0;
}

function buildTree(array &$elements, $parentId = 0)
{
    $branch = array();
    foreach ($elements as &$element) {
        $parent_id = determineParentId($element['dropdown_id']);
        if ($parent_id !== 0 && $elements[$parent_id] == null)
            unset($element["parent_id"]);        
        if ($parent_id == $parentId) {
            $dropdown = buildTree($elements, $element['dropdown_id']);
            if ($dropdown) {
                $element['dropdown'] = $dropdown;
            }
            $branch[$element['dropdown_id']] = $element;
            unset($element);
        }
    }
    return $branch;
}

var_dump(buildTree($flat));

The resulting array looks like this:

Array
(
    [1] => Array
        (
            [nome] => sub funcao teste 1
            [sub_funcao] => sub_funcao
            [nome_funcao] => 
            [tipo_sub_funcao] => 3
            [check_dropdown] => 
            [posicao] => 1
            [dropdown_id] => 1
            [pagina] => ddc_funceos
            [dropdown] => Array
                (
                    [1.1] => Array
                        (
                            [nome] => sub teste 1
                            [sub_funcao] => sub_funcao
                            [nome_funcao] => teste
                            [tipo_sub_funcao] => 3
                            [check_dropdown] => 
                            [posicao] => 5
                            [dropdown_id] => 1.1
                            [pagina] => ddc_funceos
                            [dropdown] => Array
                                (
                                    [1.1.1] => Array
                                        (
                                            [nome] => sub funcao teste 2
                                            [sub_funcao] => sub_funcao
                                            [nome_funcao] => teste
                                            [tipo_sub_funcao] => 1
                                            [check_dropdown] => 
                                            [posicao] => 1
                                            [dropdown_id] => 1.1.1
                                            [pagina] => ddc_funceos
                                        )

                                )

                        )

                    [1.2] => Array
                        (
                            [nome] => sub funcao dropdown
                            [sub_funcao] => sub_funcao
                            [nome_funcao] => funcoes_adicionar[sub_funcao]
                            [tipo_sub_funcao] => 1
                            [check_dropdown] => adicionar
                            [posicao] => 3
                            [dropdown_id] => 1.2
                            [pagina] => ddc_funceos
                        )

                    [1.3] => Array
                        (
                            [nome] => sub funcao tab
                            [sub_funcao] => sub_funcao
                            [nome_funcao] => funcoes_adicionar[sub_funcao_tab]
                            [tipo_sub_funcao] => 1
                            [check_dropdown] => adicionar
                            [posicao] => 2
                            [dropdown_id] => 1.3
                            [pagina] => ddc_funceos
                        )

                )

        )

    [3] => Array
        (
            [nome] => deletar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_deletar[sub_funcao]
            [tipo_sub_funcao] => 1
            [check_dropdown] => 
            [posicao] => 3
            [dropdown_id] => 3
            [pagina] => ddc_funceos
        )

    [2] => Array
        (
            [nome] => editar
            [sub_funcao] => sub_funcao
            [nome_funcao] => funcoes_editar[sub_funcao]
            [tipo_sub_funcao] => 3
            [check_dropdown] => 
            [posicao] => 2
            [dropdown_id] => 2
            [pagina] => ddc_funceos
        )

)
Jacob Mulquin
  • 3,458
  • 1
  • 19
  • 22
  • Thanks for the reply Jacob, i will work on it this week, and i will comeback with an answer if it helps or not. But just for know, i really appreciate the answer!!! – lucca.germano Oct 17 '22 at 22:16
  • Hello Jacob, i could achieve the nested array with the recursive function, but i used the function from the post that you tagged. Thanks a lot for the solution, i put a parent_id point to the id that i want to get nested with. One question, how i could do a
    • list from this array that was returned?
    – lucca.germano Oct 20 '22 at 19:45
  • my mistake, i just tested with on more level nested, and the execution time was to long – lucca.germano Oct 20 '22 at 20:35