Skip to content
Todo se AprendeAyuda y trucos para programación
  • Home
  • Categorías
    • Debian
    • JS
    • PHP
    • WordPress
    • MySql
    • Cordova

Build Tree to flat array PHP

02/07/2020 0 comments Article 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 vote
Article Rating
Tags: array, array to flat, build, buildTree, flat, php, tree
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Entradas recientes

  • Tabla SQL población España
  • Validar DNI (NIF), CIF, NIE
  • Table tr collapsible Jquery
  • Build Tree to flat array PHP
  • Build Tree Array PHP

Categorías

  • Cordova
  • Debian
  • JS
  • MySql
  • PHP
  • Sin categoría
  • WordPress

Archivos

Copyright Todo se Aprende 2021 | Theme by ThemeinProgress | Proudly powered by WordPress

wpDiscuz
Utilizamos cookies para asegurar que damos la mejor experiencia al usuario en nuestro sitio web. Si continúa utilizando este sitio asumiremos que está de acuerdo.Estoy de acuerdoLeer más