Build Tree to flat array PHP

Build Tree to flat array PHP

Como pasar un array arbol a plano manteniendo el orden y la estructura.
Lo unico que aquí no he podido hacerla todo lo generica que podria ser si no que es individual para cada array, ya que hay que indicarle los campos que queremos traspasar.
Si alguien tiene la solución que deje un comentario y modifico la función.

/**
  * A partir de un build tree pasarlo a flat (plano)
  */
function walkTreeToFlat($elements){
  $return = array();
  foreach($elements as &$value){
    $value['name_complete'] = str_repeat("- ", $value['level']).$value['name'];

    $return[] = array(
        'name_c'  => $value['name_complete'],
        'id2'     => $value['id2'],
        'id3'     => $value['id3'],
        'id4'     => $value['id4'],
        'id5'     => $value['id5'],
        'id6'     => $value['id6'],
        'idetc..' => $value['idetc..']
      );

    if(isset($value['children'])):
      $return = array_merge($return,$this->walkTreeCategoria($value['children']));
    endif;
  }
  return $return;
}

Aquí esta de como crear un build tree (árbol) link.

0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments